javascript - scope change doesn't execute code in directive -
here directive looks like
//noinspection jscheckfunctionsignatures angular.module('notificationdirective', []).directive('notification', function ($timeout) { //noinspection jsunusedlocalsymbols return { restrict: 'e', replace: true, scope: { ngmodel: '@' }, template: '<div class="alert fade" bs-alert="ngmodel"></div>', link: function (scope, element, attrs) { scope.$watch('message', function () { console.log('message now: ' + json.stringify(scope.message)); // element.show(); // //noinspection jsunresolvedfunction // $timeout(function () { // //element.empty(); // element.fadeout("slow"); // }, 2000); }); } } });
here controller
function notificationcontroller($scope) { $scope.message = {}; console.log('notification activated'); $scope.invite = function() { console.log("submitting invite " + $scope.email); $scope.message.title = 'hello'; // console.log('message ' + json.stringify($scope.message, null, 2)); } }
here how use it
<form class="pure-form" data-ng-controller="notificationcontroller"> <input type="text" class="pure-input-1-2" placeholder="email" data-ng-model="email"> <button type="submit" class="pure-button notice pure-button-xlarge" data-ng-click="invite()">invite me </button> </form> <notification data-ng-model="message"></notification>
what want
- whenever value if scope.message
changes in notificationcontroller
, directive has new value can alert on web page
what not understand
seems not understanding how $scope.$watch
working
please help
you made several mistakes :
- your notification tag must inside controller (in html) since must have access 'message' variable.
- your binding in directive wrong : must use '=' instead of '@' (as thalis said).
- variable 'message' not exist in directive, must use scope.ngmodel (which binded message variable).
- callback given in watcher executed each time variable updated. since use object, watcher executed when variable reference change. must set 'true' third parameter of watcher check object equality).
here sample :
html :
<body> <div id="my-app" data-ng-app="myapp"> <form class="pure-form" data-ng-controller="notificationcontroller"> <input type="text" class="pure-input-1-2" placeholder="email" data-ng-model="email" /> <button type="submit" class="pure-button notice pure-button-xlarge" data-ng-click="invite()">invite me </button> <notification data-ng-model="message"></notification> </form> </div> </body>
javascript :
var myapp = angular.module('myapp', []); myapp.directive('notification', function() { return { restrict: 'e', replace: true, scope: { ngmodel: '=' }, template: '<div class="alert fade" bs-alert="ngmodel"></div>', link: function (scope, element, attrs) { scope.$watch('ngmodel', function () { console.log('message now: ' + json.stringify(scope.ngmodel)); // element.show(); // //noinspection jsunresolvedfunction // $timeout(function () { // //element.empty(); // element.fadeout("slow"); // }, 2000); }, true); } } }); myapp.controller('notificationcontroller', ['$scope', function($scope) { $scope.message = {}; console.log('notification activated'); $scope.invite = function() { console.log("submitting invite " + $scope.email); $scope.message.title = 'hello'; // console.log('message ' + json.stringify($scope.message, null, 2)); }; }]);
see fiddle : http://jsfiddle.net/77uca/15/
Comments
Post a Comment