Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating template of child scope from parent scope in angular js
    primarykey
    data
    text
    <p>I have <a href="http://jsfiddle.net/ADukg/1403/" rel="nofollow">this fiddle example</a> where I am trying to set a value in an ng-repeat that lives in a different scope. This is a very basic example of a much bigger issue I am trying to solve. Basically I need to set a variable in the ng-repeat so that angular will update the template accordingly. The problem is that the template is in a child controller. So i use the $controller inject-able to access the variable. However, updating this variable does not cause the template to update. Even if I do a scope.$apply(). Anyone have any ideas? I am unsure on another way to do this...</p> <pre><code>var myApp = angular.module('myApp', []); myApp.directive("custdirective", function() { return { restrict: 'A', scope: 'false', link: function(scope, element, attr) { element.on("click", function() { anotherscope.addList(); }); } } }); function AnotherController($scope) { $scope.listtwo = new Array(); $scope.addList = function() { $scope.listtwo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; } } function MyCtrl($scope, $controller, $rootScope) { anotherscope = $rootScope.$new(); $scope.anothercontroller = $controller(AnotherController, { $scope: anotherscope }); }​ </code></pre> <p>To do this correctly, one would create a service. I made an updated fiddle of the correct way to do this <a href="http://jsfiddle.net/ADukg/1412/" rel="nofollow">here</a> or:</p> <pre><code>var myApp = angular.module('myApp', []); myApp.factory("mySharedService", function($rootScope) { var sharedService = {}; sharedService.message = ''; sharedService.prepForBroadcast = function() { this.message = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; this.broadcastItem(); }; sharedService.broadcastItem = function() { $rootScope.$broadcast('handleBroadcast'); }; return sharedService; }); myApp.directive("custdirective", function() { return { restrict: 'A', scope: 'false', link: function(scope, element, attr) { element.on("click", function() { debugger; scope.handleClick(); }); } } }); function AnotherController($scope, sharedService) { $scope.listtwo = new Array(); $scope.addList = function() { $scope.listtwo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; } $scope.$on('handleBroadcast', function() { $scope.listtwo = sharedService.message; $scope.$apply(); }); } function MyCtrl($scope, sharedService) { $scope.handleClick = function() { sharedService.prepForBroadcast(); }; } MyCtrl.$inject = ['$scope', 'mySharedService']; AnotherController.$inject = ['$scope', 'mySharedService'];​ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload