0

親コントローラーのスコープ変数は、トランスクルードされたディレクティブ スコープの関数内からどのように更新されますか?

次の方法でトランスクルージョンを使用して、ディレクティブを別のディレクティブに埋め込んでいます。

<my-table>
  <my-getter-button></my-getter-button>
</my-table>

my-table と my-getter-button のコードは次のとおりです。

my-tableテンプレート:

<table>
  <tr ng-repeat="item in items">
    <td data-id="{{item.Id}}" ng-transclude></td>
  </tr>
</table>

my-table指令:

.directive('myTable', function () {
      return {
          transclude: true,
          restrict: 'E',
          templateUrl: 'views/mytable.html',
          scope: false
      };
  });

my-getter-buttonディレクティブ (テンプレート付き):

app.directive('myGetterButton', function () {
    function link(scope, element) {
        scope.finalizeGet = function () {
            var id = element.parent().data('id');
            scope.clear();  // <-- works fine (from parent controller)
            scope.get(id)  // <-- works fine (from parent controller)
            .success(function (data) {
                // The line below was supposed to 
                // update the variables within the parent controller:
                scope.$parent.instance = data; // or scope.instance = data;
                angular.element('#detailsModal').modal('show');
            })
            .error(function (data) {
                scope.errors = data;
            });
        };
    };

    return {
        restrict: 'E',
        template: '<button class="btn btn-info" ng-click="finalizeGet()">' +
                      '<span class="glyphicon glyphicon-book"></span>' +
                  '</button>',
        scope: false,
        link: link
    };
});

scope.$parent.instance = data; // or scope.instance = data;親コントローラーを変更することを期待していましたが、変更されscope.instanceませんでした。

4

1 に答える 1

0

私は自分の問題の解決策を見つけました。私が必要としていたのはセッター関数でした。コントローラーに次を追加しました。

// Set current model instance of scope.
$scope.setInstance = function(data) {
  $scope.instance = data;
};

次のように、セッター関数を呼び出します (割り当て操作を行う代わりに)。

// ...
.success(function (data) {
  scope.setInstance(data);
  angular.element('#detailsModal').modal('show');
})
// ...

親コントローラースコープの変数を更新しました。

于 2015-03-05T08:47:24.560 に答える