J.Done
J.Done

Reputation: 3033

angular - stop two way binding or local value

I would like to NOT bind javascript local value to $scope function automatically, but angular did.

javascript code

    var startat = true;
    $scope.chagneStart = function() { startat = !startat}
    $scope.isStart = function() { return angular.copy( startat); }

html code

<checkbox ng-click="chagneStartAt()">
<tr ng-show="isStartAt()"> ... </tr>

When I cliked checkbox, tr tag would be show or not depend on start value. I want to NOT change tr tag until call some other function, like applyOption().

Upvotes: 0

Views: 89

Answers (2)

Surjeet Bhadauriya
Surjeet Bhadauriya

Reputation: 7156

The problem is that you are calling the function inside <tr></tr> tag (ng-show="isStartAt()") which is incorrect here.

Actually, what happens, it calls the function repeatedly. You can check this by keeping console.log('') in your function named isStartAt().

So when you click on checkbox, the function chagneStart() gets called. And it updates the value of var startat. And because your tag function isStartAt() is calling repeatedly, so it also returns the updated value of startat.

So just replace your code with the below one.

Controller

 var startat = true;
 $scope.chagneStart = function() { startat = !startat}
 $scope.isStart = angular.copy(startat);

HTML

<checkbox ng-click="chagneStartAt()">
<tr ng-show="isStartAt"> ... </t

Upvotes: 1

Slava Utesinov
Slava Utesinov

Reputation: 13488

At this case you should have oldStartat variable:

Javascript:

var startat, oldStartat = true;    
$scope.chagneStart = function() { startat = !startat}
$scope.applyOption = function() { oldStartat = startat; }

HTML:

<checkbox ng-click="chagneStart()">
<tr ng-show="oldStartat">...</tr>
<input type='button' ng-click='applyOption()' value='Apply'>

Upvotes: 1

Related Questions