0

I'm new to angularjs v1. I want to know why I cannot assign a single variable scope into the md-checkbox. like

<md-checkbox ng-click="checkAllBox();" ng-model="chkValue" aria-label="Checkbox" style="float:left;margin-left:-4px;" class="md-primary"></md-checkbox>

;controller

$scope.chkValue = false;

which doesn't work (the $scope.chkValue didn't associate with the UI checkbox correctly). But the below is ok. May I know why?

<md-checkbox ng-click="checkAllBox();" ng-model="data.cb1" aria-label="Checkbox" style="float:left;margin-left:-4px;" class="md-primary"></md-checkbox>

;controller

$scope.data.cb1 = false;
2

1 Answer 1

0

Because you are changing a variable from child scope the parent scope variable never changed.. Scope's has inheritance. You can get information from docs

Edit

To be more spesific. Controllers, some directives('ng-if', 'ng-include', vs.), isolate scopes creates its own scope.

If you want to referance a variable you need to use it with variables owner.

There is a live answer below. You can see controller has created its own scope and the parent scope cant detect changes because it has a different owner(this) now.

var app = angular.module("app",[]);
app.controller("ctrl", function($scope){
  $scope.checkData = false;
})
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>
  <body  ng-app="app"  >
    parent1:<input ng-model="checkData" type="checkbox"/>
    
      <div class="child">
        <div class="child">
          <div class="innerChild">
            inner1:<input ng-model="checkData" type="checkbox"/>
          </div>
            <div class="innerChild">
              inner2:<input ng-model="checkData" type="checkbox"/>
          </div>
        </div>
      </div>
    Controller Scope -><br>
parent2:<input ng-model="checkData" type="checkbox"/>
    <div ng-controller="ctrl" >
      <div class="child">
        <div class="child">
          <div class="innerChild">
            inner1:<input ng-model="checkData" type="checkbox"/>
          </div>
            <div class="innerChild">
              inner2:<input ng-model="checkData" type="checkbox"/>
          </div>
        </div>
      </div>
      <div class="child">
        <div class="innerChild">
          inner3:<input ng-model="checkData" type="checkbox"/>
        </div>
      </div>
    </div>
  </body>
</html>

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

2 Comments

but why it works when i add change $scope.a to $scope.something.a ?
Because if you have referance to $scope.something.a then you can change something and the changes will be reflected but $scope.a is directly variable of $scope and if you change the a, ` $scope` must be in transclude tree.

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.