gpt4 book ai didi

javascript - Angularjs 我可以从另一个 Controller 更改 Controller 的 $scope 值吗?

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:34 24 4
gpt4 key购买 nike

我有一个 HTML div ,比如

<div id="loader-container" data-ng-controller="LoaderController" ng-show="shouldShow">
Some html design
</div>

在我的 Controller 中

angular.module('core').controller('LoaderController', ['$scope','$location', 'Authentication', '$rootScope',
function($scope,$location, Authentication, $rootScope) {
$scope.shouldShow = true;
}

]);

现在,我想从另一个 Controller 隐藏那个 html div,这就是为什么我试图从另一个 Controller 将 $scope.shouldShow 变量设置为 false。那么我该如何制作

$scope.shouldShow = false;

来自另一个 Controller 。是否可能或有任何替代方法。?

最佳答案

每个 Controller 都会创建一个新的范围。

因此,更改一个 Controller 中的范围不会反射(reflect)在其他 Controller 的范围中。

如果您想在 Controller 之间共享一个属性,请在 $rootScope 上创建该变量并使用它。

在 Controller 中

$scope.$root.shouldShow = true; //or false

在标记中

<div ng-show="$root.shouldShow"></div>

关于javascript - Angularjs 我可以从另一个 Controller 更改 Controller 的 $scope 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28806321/

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