gpt4 book ai didi

javascript - Angular JS ngShow 和 ngHide

转载 作者:行者123 更新时间:2023-11-30 08:01:04 25 4
gpt4 key购买 nike

我不明白为什么 ngShow 和 ngHide 指令不起作用。这是问题的简化版本。

<div id="callFunction" ng-click="myFunction()">content here</div>
<div id="contactInfo" ng-show="showContent">content here</div>

在 Controller 中

$scope.showContent = false;

$scope.myFunction = function() {
$scope.showContent = true;
}

当我单击“callFunction”div 时,“contactInfo”div 从未显示。

最佳答案

您的问题在 JavaScript 世界中被称为“点”。这个想法是,在 javascript 中, native 值按值传递,而对象值按引用传递。尝试将您的 View 更改为:

<div id="callFunction" ng-click="myFunction()">...</div>
<div id="contactInfo" ng-show="content.Show">...</div>

并将您的 Controller 更改为:

$scope.content.Show = false;

$scope.myFunction = function() {
$scope.content.Show = true;
}

之所以可行,是因为您现在正在传递一个对象并操纵一个对象,而不是仅仅操纵一个值。本质上,将其视为“子范围”问题,您的“div”在 View 下生成自己的范围,以便您引用的变量是 native 类型,它按值传递。因此该函数更新父值而不是子值。

更多信息:http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

关于javascript - Angular JS ngShow 和 ngHide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com