I'm reading a angularJS book, and found a not working sample code =/. I'm searching but didn't find an answer. Basically I'm testing the "controller" option of a directive, the code uses a template that have a expression that evaluate a value ("goals") of the directive controller scope. But the value is not being update, I don't know why.
Here is the code: (fiddle)
HTML
<div ng-app="myApp">
<div goal game time="Cruze vs Holy" ></div>
</div>
JS
var app = angular.module('myApp', []);
app.directive('game', function () {
return {
scope: {
times: '@'
},
controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
$scope.goals = 0;
this.increment = function() {
$scope.goals++;
console.log($scope.goals);
alert("goals = " + $scope.goals);
}
}],
template: "<p>Match: [{{times}}]</p><b>Match goals: </b> {{goals}}"
};
});
app.directive('goal', function() {
return {
require: 'game',
link: function (scope, el, attrs, gameController) {
var b = angular.element("<p><button>Increase Match goals</button></p>");
b.on('click', function() {
gameController.increment();
});
el.append(b);
}
};
});