AngularJS: update directive value in directive combined with controller
Good sample controller and directive combined. From: http://jsfiddle.net/mgSpY/63/ and https://stackoverflow.com/questions/18667388/change-attribute-from-within-directive
by greatCar
HTML
<body ng-app="app" ng-controller="appCtrl">
<test-directive activate="activate.initialValue"></test-directive>
</body>
JavaScript
var app = angular.module('app', []);
app.controller('appCtrl', function($scope) {
$scope.activate = {
initialValue: 2
}
});
app.directive('testDirective', function($timeout) {
return {
restrict: 'E',
scope: {
activate: '='
},
link: function(scope, elem, attrs) {
scope.$watch('activate', function(newValue, oldValue) {
console.log('activate has changed', newValue);
});
$timeout(function() {
scope.activate = 0;
}, 2000);
},
template: "{{activate}}"
}
});