codebrew
codebrew

Reputation: 387

Update ng-model value by ng-click function will give a console error

I was using angularjs 1.6 and the browser console keep give the Error: [$parse:syntax] when I enter the page.

and some message like this.

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

The default value is true when I enter the page and I can set it off by click the switch button.

Functionality is work fine. but this error is always prompt in console and I don't know why.

In 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>

Can somebody give a hint? I guess the issues in on ng-model and ng-click.

Upvotes: 0

Views: 662

Answers (1)

Mohamed Abbas
Mohamed Abbas

Reputation: 2278

You can simply modify your variable directly in ng-click.

Example:

Try to click on Hello, World! statement.

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>

Remember: Use ng-model with input, select & textarea.

Upvotes: 2

Related Questions