Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-init is not working with ng-if

I have a div

<div class="error-text" ng-if="customerIdLength > customerIdMaxLength" ng-init="error='yes'" >Error presented</div>

here ng-if is working properly.

But

Based on ng-init value i am disabling a button like this:

<button class="btn btn-success" type="submit" ng-click="submitNumber();" ng-disabled="error=='yes'">Submit</button>

this is not working. If i am putting this ng-init to some other element which don't have ng-if then it is working properly. So what is the reason behind?

like image 476
Arpit Kumar Avatar asked Nov 08 '22 06:11

Arpit Kumar


1 Answers

<!DOCTYPE html>
<html data-ng-app="myApp">

<head>
 
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
 
</head>

<body data-ng-controller="testController">

  <div class="error-text" ng-init="$parent.error='yes'" ng-if="1 > 0">Error presented</div>

  <button class="btn btn-success" type="submit" ng-click="submitNumber();" ng-disabled="error=='yes'">Submit</button>

  <script>
    // Code goes here

    angular
      .module('myApp', [])

    .controller('testController', ['$scope',
      function($scope) {

      }
    ])
  </script>

</body>

</html>

you need to use $parent to get inner scope inside ng-if.

like image 100
Nikhil Mohanan Avatar answered Nov 15 '22 12:11

Nikhil Mohanan