1

这是我的问题:当我双击数组的一行时,我想让页面的几个部分消失。问题是......我不知道该怎么做。

基本上,这是我的 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>

我试图在控制器“表”内这样做:

$scope.dblclick = function(){
    mainWindow.hideAlias=!mainWindow.hideAlias
}

双击时 hideAlias 的值从 false 变为 true,反之亦然。但是,页面上什么也没有发生(什么都没有隐藏)

有什么线索吗?非常感谢

编辑 :

控制器定义:函数表($scope,$http,$route){

4

2 回答 2

1

您可以通过简单的方式实现这一点。

在您的案例控制器中, mainWindow 是父控制器和控制器, table 是子控制器。

为父控制器创建一个对象,并在双击事件时访问或更改子控制器的值。

var app = angular.module('myapp',[]);
app.controller('mainWindow',function($scope){
    $scope.parentobj = {};
    $scope.parentobj.hideAlias = false;
});
app.controller('table',function($scope){
    $scope.dblclicktest=function()
    {
        $scope.parentobj.hideAlias=true;
    }
});

并使用 html 中的父对象范围来隐藏 Div

<div id="mainWindow" ng-hide="parentobj.hideAlias" ng-controller="mainWindow">

这是JSFiddle

在 JSFiddle 中,双击AA将隐藏 div。

于 2014-03-27T18:22:44.307 回答
1

the variable hideAlias doesn't exist on the mainWindow controller. What you want to do is share data between the mainWindow controller and the table controller.

There's a few ways of doing this, I'll show you one

Sharing data between controllers via Event emmiters

At high level, controller Table will send data to Controller MainWindow, and controller Table is child of controller MainWindow, so here's how you do it with event emmiters:

    Controller mainWindow:


    $scope.$on('EventFromTableController',function(data){

          $scope.hideAlias = data.hideAlias;

    });

This will tell controller mainWindow to listen for the EventFromTableController event. That event will contain data attached. In this case it will hold the hideAlias value from the child controller.

Now at controller Table:

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

    }

so when dblclick executes, it will send the new hideAlias value to the parent controller (mainWindow).

This way, ng-hide will have a hideAlias scope variable to evaluate it's state.

于 2014-03-27T17:43:56.453 回答