AngularJS ng-show不能使用ng-click?

时间:2016-03-11 10:44:09

标签: angularjs

我不明白当我点击我的按钮时,我的ng-show无法正常工作... 谢谢你的帮助。

<div ng-show="showMe == 1">
     <h5>Ajouter</h5>
     <input type="texte">
</div>
<table>
    <thead>
        <tr>
            <th>Numéro :</th>
            <th>Type de Produit :</th>
        </tr>
    </thead>
    <tbody ng-repeat="product in shopCtrl.tableProduct">
        <tr>
            <td>{{product.id}}</td>
            <td>{{product.name}}</td>
            <td class="text-right">
                <div>
                    <button ng-click="showMe = 1">Ajouter</button>
                </div>
            </td>
    </tbody>
</table>

3 个答案:

答案 0 :(得分:1)

gtlambert的答案是真的。但是,如果您有多个ng-repeat级别或其他指令执行同样的操作,那么您将遇到麻烦。

使用这样的对象没有任何问题:

$scope.params = {showMe:0};// init in controller

<div ng-show="params.showMe == 1">
<button ng-click="params.showMe = 1">

无论您使用的ng-repeat /指令的数量是多少,这都将无效。

答案 1 :(得分:0)

使用ng-repeat时,会创建一个新范围。要从ng-repeat内部访问主控制器范围,您需要使用$parent

因此,将ng-click更改为$parent.showMe = 1,这样可以解决问题:

<button ng-click="$parent.showMe = 1">Ajouter</button>

答案 2 :(得分:0)

你走了。我有一个工作的例子。 showMe成为您的控制器的成员。

&#13;
&#13;
function ShopController() {
  this.showMe = false;
  this.tableProduct = [{
    id: 1,
    name: "Bar"
  }];
}

angular.module('app', [])
  .controller('ShopController', ShopController);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ShopController as shopCtrl">
  <div ng-show="shopCtrl.showMe">
    <h5>Ajouter</h5>
    <input type="texte">
  </div>
  <table>
    <thead>
      <tr>
        <th>Numéro :</th>
        <th>Type de Produit :</th>
      </tr>
    </thead>
    <tbody ng-repeat="product in shopCtrl.tableProduct">
      <tr>
        <td>{{product.id}}</td>
        <td>{{product.name}}</td>
        <td class="text-right">
          <div>
            <button ng-click="shopCtrl.showMe = true">Ajouter</button>
          </div>
        </td>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;