ng-repeat范围的奇怪行为

时间:2015-05-30 09:05:49

标签: javascript angularjs angularjs-ng-repeat ng-repeat

我有这段HT​​ML:

<div ng-switch="view">
    <div ng-switch-when="products">
        <input class="form-control" placeholder="Search" ng-model="searchText" />
        <ul ng-show="products && products.length > 0">
            <li class="head">
                <div class="column col-select"><input type="checkbox" ng-model="$parent.allSelected" ng-change="$parent.selectAll($parent.allSelected)" /></div>
                <div class="column col-name">Name</div>
                <div class="column col-code">Code</div>
                <div class="column col-stock">Stock</div>
                <div class="column col-code">Price</div>
                <div class="column col-buttons"></div>
            </li>
            <li ng-repeat="p in ($parent.shownProducts = (products | filter: searchText))">
                <div class="column col-select"><input type="checkbox" ng-model="p.selected" ng-change="select(p.selected)" /></div>
                <div class="column col-name">{{ p.name }}</div>
                <div class="column col-code">{{ p.code }}</div>
                <div class="column col-stock">{{ p.stock }}</div>
                <div class="column col-code">{{ p.price | currency: '$' : 2 }}</div>
                <div class="column col-buttons">
                    <button class="btn btn-edit" ng-click="goTo('edit', $index)"><i class="fa fa-pencil"></i></button>
                    <button class="btn btn-delete" ng-click="goTo('delete', $index)"><i class="fa fa-trash"></i></button>
                </div>
            </li>
        </ul>
    </div>
</div>

在我的Angular控制器中:

$scope.products = [{ name: 'Test1' }, { name: 'Test2' }];

$scope.select = function (value) {
    if (!value) {
        $scope.allSelected = value;
        return;
    }

    var selCount = 0;
    for (var i in $scope.shownProducts) {
        if ($scope.shownProducts[i].selected) selCount++;
    }

    if (selCount == $scope.shownProducts.length) $scope.allSelected = true;
};

$scope.selectAll = function (value) {
    for (var i in $scope.shownProducts) {
        $scope.shownProducts[i].selected = value;
    }
};

这段代码可以工作但是:

  • 标头(li.head)必须使用$ parent范围来访问$ scope服务 否则,虽然设置了初始值,但模型不会更新 正确。
  • Ng-repeat范围不使用$ parent但它可以正常工作

对我来说看起来不一致。我知道ng-repeat会创建一个子范围,但为什么它不需要$ parent呢?为什么需要$ parent,尽管它不在ng-repeat定义之外?

更新

我发布的代码是ng-switch的一部分,所以我也把它放了。 shownProducts 是一个数组,其中包含按搜索文本过滤的项目。我将该控件添加到HTML。我还在$ scope中添加了一系列产品。

2 个答案:

答案 0 :(得分:0)

DEMO - http://plnkr.co/edit/0TpaUYqgaWYlGTdQ2KJN?p=preview

<强> JS

$scope.select = function (value) {
    if (!value) {
        $scope.allSelected = value;
        return;
    }

    var selCount = 0;
    for (var i in $scope.shownProducts) {
        if ($scope.shownProducts[i].selected) selCount++;
    }

    if (selCount == $scope.shownProducts.length) $scope.allSelected = true;
  };

  $scope.selectAll = function (value) {
      for (var i in $scope.shownProducts) {
          $scope.shownProducts[i].selected = value;
      }
  };

  $scope.products = [{"name":1, "code":"001","stock":"abc","price":500},{"name":2, "code":"002","stock":"xyz","price":1000}];
  $scope.shownProducts = $scope.products;

<强> HTML

无需使用$parent。删除它,一切正常。

<ul ng-show="products && products.length > 0">
  <li class="head">
      <div class="column col-select"><input type="checkbox" ng-model="allSelected" ng-change="selectAll(allSelected)" /></div>
      <div class="column col-name">Name</div>
      <div class="column col-code">Code</div>
      <div class="column col-stock">Stock</div>
      <div class="column col-code">Price</div>
      <div class="column col-buttons"></div>
  </li>
  <li ng-repeat="p in shownProducts = (products | filter: searchText)">
      <div class="column col-select"><input type="checkbox" ng-model="p.selected" ng-change="select(p.selected)" /></div>
      <div class="column col-name">{{ p.name }}</div>
      <div class="column col-code">{{ p.code }}</div>
      <div class="column col-stock">{{ p.stock }}</div>
      <div class="column col-code">{{ p.price | currency: '$' : 2 }}</div>
      <div class="column col-buttons">
          <button class="btn btn-edit" ng-click="goTo('edit', $index)"><i class="fa fa-pencil"></i></button>
          <button class="btn btn-delete" ng-click="goTo('delete', $index)"><i class="fa fa-trash"></i></button>
      </div>
  </li>
</ul>

答案 1 :(得分:0)

问题是ng-switch创建了一个新范围,因此我必须使用$ parent来访问声明所有内容的父范围。

angularjs - ng-switch does not bind ng-model