<section ng-app="app" ng-controller="ctrl">
<p ng-show="check && form.$invalid"></p>
<form name="form" ng-submit="init()">
<input type="text" ng-model="text" required/>
<!-- <input type="submit" ng-click="alterCheck()"/> -->
<input type="submit" ng-click="check=true"/>
</form>
</section>
angular.module('app', [])
.controller('ctrl', function($scope){
$scope.check = false;
$scope.init2 = function() {
$scope.text = '';
}
$scope.init = function() {
$scope.init2();
$scope.check = false;
}
$scope.alterCheck = function() {
$scope.check = true;
}
})
In actual projects, using ng-click="check=true"
will cause p
to be displayed after clicking, but ng-click="alterCheck()"
will not. What is the reason for this?
The above code cannot reproduce the phenomenon I mentioned. Are there any additional reasons that might cause this?
I can reproduce a problem similar to what you described in this example (because I have encountered it before, so I looked at it a few more times):
If you run the current code, you will find that
{{ checkIndex }}
There is no change at all. No matter how hard you try, it will be useless to poke the mouseBut if
template
is replaced with the part of code I commented out, and thechangeCheckIndex
method is used instead,template
换成我注释掉的那部分代码,改用changeCheckIndex
方法,{{ checkIndex }}
it will change.Then the question is, how did this situation occur? We still have to go back to the documentation (I recently discovered that
ng
’s documentation is pretty good):Document address: ngRepeat
The difference between value copying and object reference is difficult to answer. I can’t reproduce the error, so I can only give a rough idea.