2014-07-24 97 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/