Usage:
<my-directive my-var="true"></my-directive>
Directive:
app.directive('myDirective', [
function () {
var definition = {
restrict: "E",
replace: false,
transclude: false,
scope: {
myVar: '@',
},
controller: ['$scope', function($scope) {
console.log($scope.myVar); // "true"
$scope.myVar = "false";
console.log($scope.myVar); // "false"
setTimeout(function() {
console.log($scope.myVar); // "true" (!)
}, 100);
}]
};
return definition;
}
]);
Console output
"true"
"false"
"true"
What is exactly happening here? The variable is passed as string ("true"), I'm changing it, then it get's replaced again? I would like to understand the cycle here. Is this because there is some additional compilation or digest cycle kicking in and recalculating all isolated scope values again? I thought that once set, variables passed like this (@, just string in directive) would stay the same?
Is there any way to hook up within directive to a moment, after which string variables are not replaced, or will it always work like this with every digest or whatnot, and I'm forced to use $watch?