gpt4 book ai didi

javascript - Angular 显示/隐藏 ng View 外的 div

转载 作者:行者123 更新时间:2023-11-27 23:14:42 25 4
gpt4 key购买 nike

试图在 ng-view 之外隐藏/显示 div 但无法访问它。

主 Controller

var vm = this;
vm.showMyDiv = false;

html

<body ng-app="app" ng-controller="ManinCtrl as main">
<div ng-show="vm.showMyDiv">
my test div
</div>
<div class="ng-view"></div>
<body>

ng View Controller

var vm = this;
vm.hideLoader = true;

我如何访问它,我尝试使用 $scope 但它不起作用

最佳答案

你正在像这样声明 Controller ng-controller="ManinCtrl as main" 所以你需要在属性前加上 main 而不是 vm

<div ng-show="main.showMyDiv">

工作示例:

angular.module('app', []).controller('mainCtrl', function() {
var main = this;
main.showMe = false;
main.toggleShow = function() {
main.showMe = !main.showMe;
};
}).controller('ctrl', function() {
var vm = this;
vm.exampleText = "I'm inner text in the controller :)";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="mainCtrl as main">
<div ng-show="main.showMe">HELLO!!</div>

<hr>

<div ng-controller="ctrl as vm">
<div ng-bind="vm.exampleText"></div>
<button ng-click="main.toggleShow()">Click Here</button>
</div>
</div>

关于javascript - Angular 显示/隐藏 ng View 外的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43807979/

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