Angular - Changing scope is not getting reflected

时间:2016-02-12 19:50:00

标签: angularjs angularjs-scope prototypal-inheritance angular-ng-if

This is weird as it should be pretty straightforward. I will post my code first and then ask the question:

html -

<div ng-controller="myController" ng-switch on="addressCards">

    <div>
        {{addCustom}} // does not get changed
        <div ng-if="addCustom === false">

            {{addCustom}} // does get changed
            <button type="button" class="btn btn-primary btn-icon-text" ng-click="addCustom = true">
                <span class="icon icon-plus"></span>
                click here
            </button>
        </div>
    </div>
</div>

controller -

(function(){
'use strict';

angular.module('myApp')
    .controller('myController',['$scope',myController]);


function myController($scope){ 

    $scope.addCustom = false;

}


})();

So I simply introduced a scope variable - addCustom - in my controller and set it to false as default. This variable controls if a div is shown or not. I am also outputting the value of the scope on the html at 2 different locations. Please see above.

But when I change its value in an ng-click within this divs, its value is changing at the second location(within the div) but not the first one(outside the div). Because of this the div does not change state as well.

I am not able to figure what might be possibly wrong here. Can someone please help?

4 个答案:

答案 0 :(得分:4)

The thing happening is when you have ng-repeat,ng-switch and ng-if directive, angular creates child scope for those element wherever they are placed. Those newly created scope are prototypically inherited from there parent scope.

On contrast Prototypal Inheritance means?

If you have scope hierarchy, then parent scope property are accessible inside child scope, only if those property are object (originally object referenced is passed to child scope without creating its new reference). But primitive datatypes are not accessible inside child scope and if you looked at your code addCustom scope variable is of primitive dataType.


Lets discuss more about it.

Here you have myController controller which has addCustom scope variable of primitive type & as I said above ng-switch & ng-if directive are compiled they do create new child scope on that element. So in your current markup you have ng-switch on ng-controller="myController" div itself. For inner html it had created a child scope. If you wanted to access parent scope inside child(primitive type) you could use $parent notation before scope variable name. Now you can access the addCustom value by $parent.addCustom. Here its not over when angular compiler comes to ng-if div, it does create new child scope again. Now inner container of ng-if will again have child scope which is prototypically inherited from parent. Unfortunately in your case you had primitive dataType variable so you need to use $parent notation again. So inside ng-if div you could access addCustom by doing $parent.$parent.addCustom. This $parent thing will solve your problem, but having it on HTML will make unreadable and tightly couple to its parent scope(suppose on UI you would have 5 child scope then it will look so horrible like $parent.$parent.$parent.$parent). So rather you should go for below approach.


Follow Dot rule while defining ng-model

So I'd say that you need to create some object like $scope.model = {} and add addCustom property to it. So that it will follow the prototypal inheritance principle and child scope will use same object which have been created by parent.

angular.module('myApp')
.controller('myController',['$scope',myController]);
 function myController($scope){ 
    $scope.model = { addCustom : false };
}

And on HTML you will use model.addCustom instead of addCustom

Markup

<div ng-controller="myController" ng-switch on="addressCards">
    <div>
        {{model.addCustom}} // does not get changed
        <div ng-if="model.addCustom === false">

            {{model.addCustom}} // does get changed
            <button type="button" class="btn btn-primary btn-icon-text" ng-click="model.addCustom = true">
                <span class="icon icon-plus"></span>
                click here
            </button>
        </div>
    </div>
</div>

Other best way to deal with such kind of issue is, use controllerAs pattern while using controller on HTML.

Markup

<div ng-controller="myController as myCtrl" ng-switch on="addressCards">
    <div>
        {{myCtrl.addCustom}} // does not get changed
        <div ng-if="myCtrl.addCustom === false">

            {{myCtrl.addCustom}} // does get changed
            <button type="button" class="btn btn-primary btn-icon-text" ng-click="myCtrl.addCustom = true">
                <span class="icon icon-plus"></span>
                click here
            </button>
        </div>
    </div>
</div>

答案 1 :(得分:2)

From the Docs:

The scope created within onsubmit="return getlocation();" inherits from its parent scope using prototypal inheritance. An important implication of this is if ngIf is used within ngModel to bind to a javascript primitive defined in the parent scope. In this case any modifications made to the variable within the child scope will override (hide) the value in the parent scope.

-- AngularJS ng-if directive API Reference

The rule of thumb is don't bind to a primitive, instead bind to an object.

Scope inheritance is normally straightforward, and you often don't even need to know it is happening... until you try 2-way data binding (i.e., form elements, ng-model) to a primitive (e.g., number, string, boolean) defined on the parent scope from inside the child scope. It doesn't work the way most people expect it should work. What happens is that the child scope gets its own property that hides/shadows the parent property of the same name. This is not something AngularJS is doing – this is how JavaScript prototypal inheritance works. New AngularJS developers often do not realize that ngIf, ng-repeat, ng-if, ng-switch and ng-view all create new child scopes, so the problem often shows up when these directives are involved. (See this example for a quick illustration of the problem.)1

This issue with primitives can be easily avoided by following the "best practice" of always have a '.' in your ng-models – watch 3 minutes worth. Misko demonstrates the primitive binding issue with ng-switch.1

答案 2 :(得分:1)

Ng-if introduces a different scope. Try this as an attribute of your button:

ng-click="$parent.addCustom = false"

This will assure that you're accessing the same scope.

答案 3 :(得分:0)

It's because of this that it's always good practice to use the python manage.py loaddata app/myapp/fixtures/* --settings=setting.develop syntax. All attributes are bound to the controller object and namespaced accordingly, meaning you never run in to this problem. I've updated your example using the ControllerAs syntax to demonstrate its use.

HTML

ControllerAs

Controller

<div ng-controller="myController as vm" ng-switch on="addressCards">
  <div>
    {{vm.addCustom}}

    <div ng-if="vm.addCustom === false">
      {{vm.addCustom}}
      <button type="button" class="btn btn-primary btn-icon-text" ng-click="vm.addCustom = true">
        <span class="icon icon-plus"></span>
          click here
      </button>
    </div>
  </div>
</div>

Here is an excellent article providing more detail about (function(){ 'use strict'; angular.module('myApp') .controller('myController', [ myController ]); function myController () { var vm = this; vm.addCustom = false; } })(); and it's advantages.

Both Classic Controller and Controller As have $scope. That's super important to understand. You are not giving up any goodness with either approach. Really. Both have their uses.