0

I've just upgraded to angularjs 1.2.1, and some code which has been working before, is no longer working.

I have the following directive:

var fooFactory = function($timeout)
{
    var foo = 
    {   
        scope:
        {
            bar: '=',
            baz: '=', 
            onBar: '&',
            onBaz: '&',
        },
        controller: ['$scope', '$element', '$attrs', 
           function($scope, el, attrs)
           {
               var that = this;      
               that.state = null;
               that.main = function()
               {
                  that.state = "init";
               };

               that.getState = function()
               {
                   return that.state;
               }


              that.main();
              $scope.foo = that;    
              console.debug( "foo main called", $scope.foo );
           }],


       link: function(scope, el, attrs)
       {
       },

    };
    return foo;
};

MyAngularApp.directive('foo', ['$timeout', fooFactory]);

Here's how I'm including this directive in the view:

<div data-foo="true">
   Foo: {{foo}} <br />
   Foo state: {{foo.getState()}}
</div>

The problem is, when I run this code, I do get the console.debug statement for foo main called(), and $scope.foo is correctly set to the controller of the foo directive. But in the view itself, I see no output at all for either {{foo}} or {{foo.getState()}}, as if these have not been set on the scope at all.

Is it because of the isolate scope, that the line $scope.foo = that; is not having any effect? Please advise.

1 Answer 1

1

Works if you update (I would rather say pollute) the one already in the parent scope:

$scope.$parent.foo = that;    
console.debug( "foo main called", $scope.$parent.foo );

Weird! It seems instead of creating a model in the directive's scope it goes up the chain looking for the model in the parent scope which I believe is not even there. Totally weird!

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain the last sentence a bit?
@ClickUpvote I dont think you have foo in the parent scope. This means doing $scope.foo = something should create a model in the directive scope. But it does not. Looks like a bug to me.
Actually it seems to be because of the isolate scope. If I remove the isolate scope from the directive, then it works. Not sure if that's a feature or a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.