1

In Angular 1.2, I have a child directive and a parent directive (with an isolate scope) to make a generic popup who takes directive in parameter and include it.

In my child directive i have to update the title of my parent directive, the value is updated in my console, but it is not updated in my UI.

Behavior:

1 - I set attributs of my directive declaration like : `

2 - I open my custom-box for audio devices by click on button -> the title equals A

3 - I have 3 tab in my audio-popup directive, so i click on 3rd tab, and i try to change the title of my custom-box -> FAIL

I ve tryed differents solutions but i can't find, i precise i can't use rootScope in this case.

//childDirective
$scope.setTabTo = function (to) {
    $scope.tab = to;
    if ($scope.tab == "already") {
       $scope.$parent.continueClose = false; //in console = false
       $scope.$apply(function () { $scope.$parent.title = "New Title" });
    }
    //...
};

I 've read this : How to access parent scope from within a custom directive *with own scope* in AngularJS? but I have an error if i try $scope.$parent.$apply directly, have miss something to apply the scope ... And this post Directive updates parent scope value doesn't works for me.

I have tryed to watch the title value in my parent directive but doesn't work to...

Community
  • 1
  • 1
Ema.H
  • 2,862
  • 3
  • 28
  • 41
  • 1
    If child directive has no isolated scope, you should just $scope.Model.title = "New Title". You do not need apply. (Model.title cause just .title wont work - you can not change parent scope root objects, only nested ones) – Petr Averyanov Sep 04 '15 at 13:17
  • You shouldn't need to `$apply` in this case. Also, `if ($scope.tab = 'already')` will always return falsey. You need `==` or better `===`. – Davin Tryon Sep 04 '15 at 13:17
  • @Petr Averyanov Thanks a lot, you have right that's the solution !! – Ema.H Sep 04 '15 at 13:26
  • did you try to use transculde ?? it gives you to control of your parent $scope. – Nadeem Khoury Sep 04 '15 at 13:30
  • yes, i've tried but in this case i 'm already in the good scope ^^ else i arrive in my rootscope and my variable doesn't exist. – Ema.H Sep 04 '15 at 13:41

2 Answers2

1

The $parent you use is not the parent you think it is. Have in mind that the audio-popup may have isolated scope which will add another child in the chain. Also there are directives which create new child scopes: ng-controller, ng-switch, ng-if, ng-repeat, etc.

I also don't see why you need scope.$apply in this case. You need to call $apply only when you are modifying the scope from outside the angular world (e.g. element.on('click') instead of ng-click).

You have given us very little code to work with and to give you more exact answer.

vbuhlev
  • 509
  • 2
  • 10
0

If child directive has no isolated scope, you should just $scope.Model.title = "New Title". You do not need apply. (Model.title cause just .title wont work - you can not change parent scope root objects, only nested ones) – Petr Averyanov

True, i've just to use $scope.title and not $scope.$parent.title

in fact, my custom-box have an isolate scope but not my audio-popup directive so i can access directly my custom-box scope !

Ema.H
  • 2,862
  • 3
  • 28
  • 41