I have a problem in understanding the scope : {}. Below is the code sample i am working on. Why does it always print in console "strength" instead of its corresponding array value.
// Code goes here
var app = angular.module("superApp", []);
app.directive("superhero", function() {
return {
restrict: "E",
controller: function($scope) {
$scope.abilities = []
this.addStrength = function() {
$scope.abilities.push("strength")
}
this.addSpeed = function() {
$scope.abilities.push("speed")
}
this.addFlight = function() {
$scope.abilities.push("flight")
}
},
link: function(scope, element) {
element.addClass("button");
element.bind("mouseenter", function() {
console.log(scope.abilities);
})
}
}
});
Below is the entire working code.
http://plnkr.co/edit/jv2xZRvx4X8IcjKKkiXU?p=preview
It always prints "strength", regardless of the hover. The moment i add scope: {}, it prints its corresponding values.
I am not understanding here, is what does the scope: {} do the magic here?. What is isolated here? I am totally confused here.
scope: {}isolates the outer scope from the inner scope, in a sense that your directive only has access to aliased variables passes explicitly to it. Read about it directly in the documentation