2014-07-24 95 views
1

我有一个表中有按钮的应用程序。理论上点击的按钮应隐藏表格并更改一些数据。当按钮在桌子外面时,它可以正常工作,但在其内部失败。这是我的代码。
HTML:按钮在表格中不起作用?

<body link="white" vlink="white"> 
    <pin>Site ID</pin> 
    <center> 
    <h1>Site Finder</h1> 
    <button ng-click="'x=0','y=0'">test</button> 
    <div ng-controller="firstCtrl"> 

     <input type="text" ng-model="search" border="3" placeholder="Please enter site name..." ng-hide="hideAttr"/> 
     <div link="white" vlink = "white"><button id="btn2" ng-click="hideAttr = !hideAttr" ng-hide="!hideAttr">Back To Search</button></div> 
     <table border="1" width="100%" ng-hide="hideAttr"> 
     <thead> 
      <tr> 
      <td><center>Name</center></td> 
      <td>Carrier</td> 

      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="site in SiteLocs | filter : search"> 
      <td> 
      <button id="btn1" ng-click="hideAttr = !hideAttr"> 
       {{site.name}} 
      </button> 
      </td> 

      <td> 
       {{site.carrier}} 
      </td> 
      </tr> 
     </tbody> 
     </table> 
    </div> 


    </center> 
    <div id="map-canvas" ng-show="!hideAttr"></div> 
</body> 

</html> 


JS:

(function(){ 

    $scope.hideAttr = true; 

}); 

为什么不会在表中按钮的工作?

回答

4

button包含在ng-repeat指令中。 ng-repeat创建它自己的子范围,所以实际发生的是您要在名为hideAttr的子范围上创建一个新的$scope变量并进行设置。一对夫妇的变通:

  1. 在控制器中定义的函数,调用 - 角将查找到父,找到方法

  2. 使用$parentng-click方法:ng-click="$parent.hideAttr = !$parent.hideAttr"

+0

当你说调用一个函数来解决它。你的意思是这样的:

+1

@IanPennebaker - Yep – tymeJV

1

As @tymeJV指出ng-repeat创建新的范围。当您在子作用域上更改原始值时,它将创建隐藏父属性的副本。在控制器,你应该有一个具有要更改即

$scope.tableAttrs = { hide: false }; 

原始属性的对象和ng-repeat标记内,你可以使用:

<div ng-hide="tableAttrs.hide">something to hide</div> 
<button ng-click="tableAttrs.hide = !tableAttrs.hide">hide</button> 

继承人博客文章,解释它(与形式但相同的想法,子范围隐藏原始值)http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/