javascript 在 ng-click 上更改范围变量而不调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30717320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:36:06  来源:igfitidea点击:

Changing scope variable on ng-click without calling a function

javascriptangularjsoperatorsangularjs-ng-clickangular-ng-if

提问by Body

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.

我试图在 editState 变量等于 1 时显示一个 div 内容,但它不起作用。

HTML

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)

JS(在 profileCtrl 控制器中)

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

回答by Pankaj Parkar

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

ng-click指令中设置值时,使用赋值运算符=而不是===精确检查运算符。

It should be

它应该是

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

Instead of

代替

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