Body Body - 6 months ago 35
AngularJS Question

Changing scope variable on ng-click without calling a function

How can I change a Scope Variable without calling a function from controller.

I'm trying to show a div content when the editState variable equals to 1 but it's not working.

HTML

<body data-ng-controller="profileCtrl as pctrl">

<div data-ng-click="pctrl.editState === 1">Edit</div>

<div data-ng-if="pctrl.editState === 1">
.....
</div>

</body>


JS(in profileCtrl controller)

this.editState = 0;


But when I called a function it's working (I don't want to do this way)

<div data-ng-click="pctrl.editFn()">Edit</div>

this.editFn = function() {
this.editState = 1;
}

Answer

While setting value inside ng-click directive, use assignment operator = instead of === exact check operator.

It should be

<div data-ng-click="pctrl.editState = 1">Edit</div>

Instead of

<div data-ng-click="pctrl.editState === 1">Edit</div>
Comments