ng-click函数更新ng模型值将产生控制台错误



i使用angularJS 1.6,浏览器控制台保留给出错误:[$ parse:stytax] 当我输入页面时。

和一些这样的消息。

<span class="switch" ng-click="disableValue() undefined ? optionValue: optionValue=!optionValue" ng-class="{ checked:optionValue, disabled:undefined }" style="margin-top:0px;" ng-model="optionValue">

当我输入页面时,默认值是正确的,我可以通过单击"开关"按钮设置它。

功能正常。但是这个错误总是在控制台中提示,我不知道为什么。

在html

angular.module('app').controller({
  $scope.optionValue = true;
  
  $scope.disableValue = function()
  {
    $scope.optionValue = !$scope.optionValue;
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<switch style="margin-top:0px;" ng-model="optionValue" ng-click="disableValue()"></switch>

有人可以提示吗?我猜ng模型和ng点击中的问题。

您可以直接在ng-click中修改您的变量。

示例:

尝试单击Hello, World!语句。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.colorVal= true;
});
.red {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <span ng-class="{red: colorVal}" ng-click="colorVal = !colorVal">
  Hello, World! </span>
</div>

记住: inputselect&amp;一起使用NG模型textarea

最新更新