2

Angular で次のようなディレクティブを作成しました。

angular.module('msfApp')
    .directive('listitem', function () {
        return {
            templateUrl: 'assets/templates/directives/listitem.html',
            restrict: 'E',
            scope: {
                'item': '='
            }
        }
    });

テンプレートは次のようになります。

<div class="tsProductAttribute" ng-click="toggleInBasket(item)">
    <span class="tsProductAttribute-image">
        <img ng-src="{{item.variants[0].image}}">
    </span>
    <span class="tsProductAttribute-desc">{{item.productName}}</span>
    <span class="tsProductAttribute-price">{{item.variants[0].price[0].amount}} {{item.variants[0].price[0].entity}}</span>
</div>

しかし今、私は2つの質問があります:

  1. コントローラーで ng-click 関数が起動しませんtoggleInBasket(item)。なぜですか?
  2. 次に、トグル動作をリスト項目に追加して、「tsProductAttribute--selected」というクラスを切り替えるにはどうすればよいですか

よろしくお願いします!

4

3 に答える 3

12

1) 問題は隔離されたスコープです。コントローラー スコープで関数を確認することはできません。1 つの解決策は、関数参照をディレクティブに渡すことです。

http://plnkr.co/edit/GorcZZppa8qcIKbQAg2v?p=preview

<body ng-controller="ItemController">
  <listitem item="item" item-click="toggleInBasket(item)"></listitem>
</body>

ディレクティブで:

scope: {
    'item': '=',
    'itemClick': '&'
}

そしてテンプレートで:

<div class="tsProductAttribute" ng-click="itemClick(item)">

2) ディレクティブに別の関数を作成して、選択した状態を切り替え、コントローラー関数を呼び出します。

angular.module('msfApp').directive('listitem', function () {
  return {
    templateUrl: 'listitem.html',
    restrict: 'E',
    scope: {
      'item': '=',
      'itemClick': '&'
    },
    link: function(scope, iElement, iAttrs) {
      scope.selected = false;
      scope.toggleState = function(item) {
        scope.selected = !scope.selected;
        scope.itemClick(item);
      }
    }
  }
});

テンプレートでクラスを切り替えます。

<div class="tsProductAttribute" 
    ng-class="{'tsProductAttribute--selected': selected}" 
    ng-click="toggleState(item)">
于 2013-07-10T10:17:14.467 に答える
1

これは、新しいスコープを作成する scope: { 'item': '=' } を使用してディレクティブで分離されたスコープを使用しているため、ng-click がコントローラー関数にバインドできないために発生しています。

ng-click を使用して親関数を呼び出すには、以下のリンクを参照してください。

AngularJS のディレクティブから親コントローラーのメソッドを呼び出す

于 2013-07-10T10:07:20.167 に答える
1

@Macrosの答えは、私にとってはうまくいきました!完成したコードは次のとおりです。

ディレクティブ テンプレート ファイル:

<div    class="tsProductAttribute" 
        ng-class="{'tsProductAttribute--selected': selected}" 
        ng-click="toggleState(item)">

    <span class="tsProductAttribute-image">
        <img ng-src="{{variantImage}}">
    </span>
    <span class="tsProductAttribute-desc">{{item.productName}}</span>
    <select ng-model="variantImage">
        <option  ng-repeat="variant in item.variants" value="{{variant.image}}">{{variant.name}} - {{variant.listprice.amount}}</option>
    </select>
    <span class="tsProductAttribute-price">{{item.variants[0].listprice.amount}} {{item.variants[0].listprice.entity}}</span>
</div>

指令:

angular.module('msfApp')
.directive('listitem', function () {
    return {
        templateUrl: 'assets/templates/directives/listitem.html',
        restrict: 'E',
        scope: {
            'item': '=',
            'itemClick': '='
        },
        link: function(scope, iElement, iAttrs) {
          scope.selected = false;
          scope.toggleState = function(item) {
            scope.selected = !scope.selected;
            scope.itemClick(item);
          }
        }
    }
});

ディレクティブの実装:

<listitem item="item" item-click="toggleInBasket"></listiten>

コントローラーの機能:

$scope.toggleInBasket = function(item) {
        $scope.basket.toggle(item);

        console.log(basket.get());

    }
于 2013-07-10T11:40:24.630 に答える