javascript - Can you update a controller that updates it's child scopes? -


lets have controller & directive so:

app.controller('maincontroller', function($scope) {     $scope.loading = true;     $scope.updatetest = function(){         $scope.loading = false;     } }).directive('loading', function() {     return {         restrict : 'e',         transclude: true, // use parent scope         link : function(scope, element, attrs) {             scope.$watch('loading', function(ctx) {                 console.log('updating?', ctx)             });         }     } }); 

and html markup:

<section ng-controller="maincontroller">     <loading ng-show="loading">         loading?     </loading>     <button ng-click="updatetest()">force update</button> </section> 

i want know how watch parent scope within directive, can't seem working!

i'm using angular 1.3.16

i recommend use isolate scope , pass variables throw it:

.directive('loading', function() {   return {     restrict : 'e',     scope: {       ngmodel: '='     },     transclude: true, // use parent scope     link : function(scope, element, attrs) {         scope.$watch('ngmodel', function(ctx) {             console.log('updating?', ctx)         });     }   } }); 

html

<loader ng-model="loading" ng-show="loading">     loading? </loader> 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -