I have a directive defined like this:
myApp.directive('stoplight', function() {
return {
restrict:'E',
transclude: true,
scope: {
value: '@'
},
link: function(scope, element) {
if (scope.value === true) {
element.html('<i class="icon icon-true green"></i>');
} else if (scope.value === false) {
element.html('<i class="icon icon-false red"></i>');
} else {
element.html('<i class="icon icon-unknown yellow"></i>');
}
}
};
});
When I use this directive, I use the following markup:
<stoplight value="boolValue" />
My controller behind stoplight looks like this:
myApp.controller('MyController', function($scope, $http) {
$scope.boolValue = null;
$scope.init = function() {
$scope.boolValue = false;
$http.jsonp('http://anyorigin.com/get?url=www.google.com&callback=JSON_CALLBACK')
.success(function() {
console.log('woohoo!');
$scope.boolValue = true;
});
};
$scope.init();
}};
I have two issues, and neither make sense to me.
- The '
@
' in my directive doesn't work. If I change the '@
' to a '=
', the link function works somewhat as expected. However, I want to use one-way binding instead of two-binding for performance reasons. - For some reason, the
$scope.boolValue = true;
in my success callback doesn't update the UI. The icon stays red. I can set the value to null, expecting yellow, but it stays red. If I look in the console window though, I can see 'woohoo!
'. I don't understand why updatingboolValue
outside of the callback works, yet in the callback, it just doesn't work. I do not see any error in the console window or anything of that nature.
Can someone please help me identify why this isn't working? I'm not sure if this is one issue or two issues. I think they both have to do with the binding. However, I'm not sure how to remedy this.
Thank you.