ng-include modify parent scope?

5,780 views
Skip to first unread message

ItsLeeOwen

unread,
May 7, 2012, 6:55:31 PM5/7/12
to ang...@googlegroups.com
I have an ng-include, which I want to update values on the parent scope.  This fiddle doesn't work, is there some way of telling the ng-include to work on the parent scope?


Even modifying $scope.$parent.test = "some value" does not work.

Suller Andras

unread,
May 8, 2012, 7:08:39 AM5/8/12
to ang...@googlegroups.com
You need to use $scope.$parent.$parent.test .
It is ugly and unpredictable, so i recommend you to wrap your data in
an object variable: http://jsfiddle.net/e5rfP/3/

Andras

ItsLeeOwen

unread,
May 8, 2012, 4:44:50 PM5/8/12
to ang...@googlegroups.com
Thanks Andras!  I have been moving most places where I'd previously used ng-include to directives, which works with parent scope as I'd expect as long as you don't inject an iso-lated scope.



Christoph Burgdorf

unread,
May 8, 2012, 5:57:30 PM5/8/12
to ang...@googlegroups.com
You might want to check out this related issue I just created:  https://github.com/angular/angular.js/issues/943 

ItsLeeOwen

unread,
May 10, 2012, 2:20:27 PM5/10/12
to ang...@googlegroups.com
Was there a solution on how to cleanly use ng-include without an isolated scope?

http://jsfiddle.net/ItsLeeOwen/V4Azc/

If I have an ng-include in a directive with an isolated-scope, I then have to use $parent.$parent.

John Lindquist

unread,
May 10, 2012, 3:09:37 PM5/10/12
to ang...@googlegroups.com
You could inject and expose a service through the scope (instead of trying to rely on scope inheritance)




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/q8_nBcTNaScJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

ItsLeeOwen

unread,
May 10, 2012, 6:08:12 PM5/10/12
to ang...@googlegroups.com
Thanks John, there seem to be so many ways to skin an angular cat  :)

I realized that I could get attributes from the controller constructor $attrs param.  That should accomplish my goal.

ItsLeeOwen

unread,
May 10, 2012, 7:08:18 PM5/10/12
to ang...@googlegroups.com
A downside to using the $attrs parameter, is that it does not evaluate keywords, or functions of the attribute.  So if you want my-attribute="{{ parentValue }}" to return the result of {{ parentValue }}, instead you'll be getting the string "{{ parentValue }}"

ItsLeeOwen

unread,
May 10, 2012, 7:32:42 PM5/10/12
to ang...@googlegroups.com
Is there anything wrong with exposing the parent scope inside of a directive with a scope hashtag "evaluate"?


        scope{
            'foo''attribute',
            'parent''evaluate'
        },

    <my-element foo="bar" parent="this"></my-element>


Andy Joslin

unread,
May 10, 2012, 7:35:54 PM5/10/12
to ang...@googlegroups.com
Isn't a directive scope's $scope.$parent already the controller that wraps it?

<div ng-controller="MyCtrl">
    <my-element><!-- $scope.$parent = MyCtrl in here--></my-element>
</div>

ItsLeeOwen

unread,
May 10, 2012, 7:44:39 PM5/10/12
to ang...@googlegroups.com
Sort of, but it gets kind of haywire.  See what's going on here:  http://jsfiddle.net/ItsLeeOwen/34DeS/

$scope.$parent.hello gives nothing.  $parent.hello works in the directive, but not in the ng-include inside of the directive.  I suppose that makes sense, but my goal is to access the normal scope inheritance chain from within the directive, while passing in attributes with the scope hashtag.  


Vojta Jína

unread,
May 20, 2012, 10:45:02 PM5/20/12
to ang...@googlegroups.com
If "my-element" directive creates new scope, then scope.$parent is
MyCtrl, otherwise scope is MyCtrl.

V.
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/vSQbRvUsWloJ.
Reply all
Reply to author
Forward
0 new messages