greenoldman greenoldman - 4 months ago 7
AngularJS Question

Are directive's properties bound in controller?

I wonder if I have some bad wiring somewhere and/or it is unsafe to assume that in controller of directive (with isolated scope) all properties are set?

Example:

.directive("wordEditor",function(){
return {
scope : {
categories : "=",
},

controller : function($scope)
{
$scope.categories; // is it safe?
...


So far it worked well, but when I started playing with nested directives I get undefined properties.

So I would like to sort out at least one thing -- do I have guarantee (for current directive) that a property is bound within controller when it is executed?

I am not asking if it has a value in global sense, because it depends what the outer world has, I am asking about what happens within given directive.

Answer

do I have guarantee (for current directive) that a property is bound within controller when it is executed?

There is no guarantee. Bound scope property may be available in child controller only if it is available in parent controller at this moment. This won't be true if the directive is hosted by ng-repeat, or parent controller uses $onInit to initialize its properties, for example.

Due to data binding nature, it should be always assumed that bound properties aren't available yet or may be changed later, they should be watched with $scope.$watch or $onChanges.