admin管理员组

文章数量:1312732

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;
}

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;
}
Share Improve this question edited Jun 8, 2015 at 19:35 isherwood 61.1k16 gold badges121 silver badges169 bronze badges asked Jun 8, 2015 at 19:17 BodyBody 3,6889 gold badges43 silver badges51 bronze badges 1
  • Typo change === to = @ data-ng-click="pctrl.editState === 1" to data-ng-click="pctrl.editState = 1". You need assignment operator (=) not parison operator (===). – PSL Commented Jun 8, 2015 at 19:18
Add a ment  | 

1 Answer 1

Reset to default 5

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>

本文标签: javascriptChanging scope variable on ngclick without calling a functionStack Overflow