这是我的问题: 当我双击数组的一行时,我想让页面的几个部分消失。问题是……我不知道该怎么做。
基本上,这是我的 html 文件:
<div id="mainWindow" ng-hide="hideAlias" ng-controller="mainWindow">
...
<div id="table{{workspace.name}}" class="table" ng-controller="table" >
<table id="mainTable" class="mainTable">
<tr class="tableHeader">
<th>AA</th>
<th>BB</th>
<th>Options</th>
</tr>
<tr class="tableRows" id ="{{row}}" ng-repeat = "row in rowstable">
<td ng-dblclick="dblclick()" >{{row.AA}} </td>
<td>{{row.server}} <input type="button" ng-click="clickOnDeleteServer(row.BB)" value="X" style="float:right"/></td>
<td>
<input type="button" ng-click="clickOnView()" value="View"></input>
<input type="button" ng-click="clickOnDelete(row.AA)" value="Delete"></input>
</td>
</tr>
</table>
</div>
...
</div>
我曾尝试在 Controller “表”内这样做:
$scope.dblclick = function(){
mainWindow.hideAlias=!mainWindow.hideAlias
}
当我双击时,hideAlias 的值从 false 变为 true,反之亦然。但是,页面上没有任何反应(没有隐藏任何内容)
有什么线索吗?非常感谢
编辑:
Controller 定义: 函数表($scope, $http, $route){
最佳答案
mainWindow Controller 上不存在变量 hideAlias。 您要做的是在主窗口 Controller 和表 Controller 之间共享数据。
有几种方法可以做到这一点,我会告诉你一个
通过事件发射器在 Controller 之间共享数据
在高层, Controller 表会将数据发送到 Controller 主窗口,而 Controller 表是 Controller 主窗口的子级,所以下面是使用事件发射器的方法:
Controller mainWindow:
$scope.$on('EventFromTableController',function(data){
$scope.hideAlias = data.hideAlias;
});
这将告诉 Controller mainWindow 监听 EventFromTableController 事件。该事件将包含附加数据。在这种情况下,它将保存来自子 Controller 的 hideAlias 值。
现在在 Controller 表:
Controller table:
var tableHideAlias = true; // initialize it to true or false
$scope.dblclick = function(){
//change the local tableHideAlias state
tableHideAlias = !tableHideAlias;
// emit the new hideAlias value
$scope.$emit('EventFromTableController',{hideAlias: tableHideAlias});
}
所以当 dblclick 执行时,它会将新的 hideAlias 值发送到父 Controller (mainWindow)。
这样,ng-hide 将有一个 hideAlias 范围变量来评估它的状态。
关于javascript - AngularJS - ng-hide 与不同的 ng-controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694536/