javascript - Setting a $scope variable in view does not work unless I call a function to do it -


if have html file toggle boolean on click, i'm able directly in view , have create function in controller toggle boolean. why work not time?

only works sometimes..

<div ng-click="myvar = !myvar">toggle</div> 

always works..

<div ng-click="updatevar();">toggle</div> ... $scope.updatevar = function() {   $scope.myvar = !$scope.myvar; }; 

if have not set $scope.myvar initially, myvar , $scope.myvar not same variable. use...

<div ng-click="$scope.myvar = !$scope.myvar"> 

...to ensure accessing same variable.