0

I'm writing an element-level directive that has a number of attributes on it. These attributes are then put into an isolate scope using the '@' modifier.

The directive is used on a page that populates the attributes with expressions i.e

<my-directive attr1="{{foo.bar}}"></my-directive>

I'm finding that when the directive controller executes, the $scope hasn't resolved the expressions yet. Is there a way to force the scope to resolve before entering the controller?

Dan
  • 3,229
  • 2
  • 21
  • 34

1 Answers1

2

No, you can't force the scope to be resolved before the controller runs. Use $observe in the controller to asynchronously get the value (and to be notified whenever the value changes -- just like $watch):

controller: function($scope, $attrs) {
    $attrs.$observe('attr1', function(newValue) {
        ....
    });
}
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • Thanks. Does this mean I should be using $attrs instead of putting my attribute values into an isolate scope? – Dan Sep 11 '13 at 13:25
  • @dskh, that depends on what you need your directive to do. I suggest reading the following answer, where I go in to much more detail about $observe and $watch, with isolate scopes and non-isolate scopes: http://stackoverflow.com/a/14907826/215945 – Mark Rajcok Sep 11 '13 at 13:34