javascript - How can i change array in directive, and then reflect that change in my controller? -


i made directive isolated scope "=" method, in directive pass empty array, push data on array.... how change can reflected on original array in controller?

here example:

angular.module('mymodule').controller('mycontroller', ['$scope', function($scope) {          $scope.test = [];      }]);  angular.module('mymodule').directive('mydirective', function() {      return {         scope: {             test: "=",             bread: "="         },         restrict: 'e',         link: function(scope, element, attribute) {              scope.test.push('one more')          },         replace: true,         templateurl: 'some template'     }; }); 

html

 <div ng-controller='mycontroller'>      <mydirective test='test'></mydirective>       <div ng-bind='test'> </div>     </div> 

when push on array dont have reflection of in controller. how can fix that?

here's how trying achieve.

html

<!-- myctrl contains original array, push here scope 'ctrl' --> <div ng-controller='myctrl ctrl'>   <!-- pass directive 'ctrl.test', tells angular two-way bind        test property on 'ctrl' object on current scope -->   <mydirective test='ctrl.test'>     <!-- we're inside isolate scope: test here refers mydirective's idea of test,          two-way bound array -->     <div ng-bind='test'></div>   </mydirective> </div> 

js

angular.module('app', [])   .directive('mydirective', function() {     scope: {       test: '='     },     link: function($scope) {       $scope.test.push('one more');     }   })   .controller('myctrl', function() {      this.test = [];   }); 

any alterations array reflected in ng-bind. please note bad practice place primitives on $scope without being part of object (due mechanics of prototypical inheritance) you'd want change $scope.test else.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -