angularjs - angular directive not listening to changes in outer scope -
I am currently trying to listen to the instructions for changes in the parent area with a specific variable I am here. For that purpose I came up with the following code which runs the controller for the first time, but variable grid sales in the second part of $ XVI is not changing.
Any able to give me an indication that I am doing wrong here?
Controller:
$ scope.gridCells = []; $ Scope.gridCells.push ({value: '1'}); $ Scope.gridCells.push ({value: '2'}); $ Scope.gridCells.push ({value: '1'}); $ Scope.gridCells.push ({value: '2'}); // This change is not displayed in $ $ timeout (function () {console.log ('push'); $ scope.gridCells.push ({value: '2'}); $ scope.gridCells.push ({ Value: '1'});}, 4000); HTML:
& lt; My Directive Cells = Grid Seals & gt; & Lt; / My-directive & gt; Director: Angular Module ('myApp'). Instructions ('myDirective', [function ()) {return {restricted: 'e', scope: {cells: '='}, link: function (region, element, attic) {scope.gridCells = []; Scope $ Watch (attrs.cells, function (newcomer, old value) {console.log ('new:' newvalue); console.log ('old:' + oldValue); (var i = 0; i & lt; Scope.cells.length; i ++) {// Populate some of my scope.gridCells variable If the content in the variable cells of the renewed cells (scope.cells [i] .value === '1') {gridCells.push ( Scope.cells [i]);}}})}, templates: '{{gridCells. Length}} '}}}]); Instead of $ watch you need to use or
A deep ( area. $ Watch (prop, func, true) ) is designed to be able to track the changes in the array, the attribute has been created by the cells cell instead of the scope of code> . $ WatchCollection ('cells', function () {// Maybe you want to reset gridclass here? //scope.gridCells = []; for (var i = 0; i & lt; scope.cells.length; I ++) {if (scope.cells [i] .value == = '1') {scope.gridCells.push (scope.cells [i]);}}});
Comments
Post a Comment