-1

私は 2 つのディレクティブを持っています。1 つは ng-grid 用で、もう 1 つはページ付け用です。1 つのディレクティブでページ番号をクリックすると、ng-grid ディレクティブはそれに応じて変更する必要があります。

4

1 に答える 1

0

それを達成する方法はたくさんあります: 例えば:

最初の解決策

ディレクティブ間でデータを共有できます。

<directive-one attribute="value" other-attribute="value2" shared-variable="yourData">
<directive-two shared-variable="yourData">

そして$watch、その値の最初のディレクティブ内に設定します

scope.$watch('yourData',function(newVal,oldVal){ //your logic called after change });

2 番目の解決策

イベントを使用できます:

app.directive('first',function(){
    return{
        restrict: 'E',
        template: 'Im first directive!',
        scope: true,
        link:function(scope,elem,attrs){
            scope.$on('event',function(event,args){
               alert(args); 
            });
        }
    }    
});

app.directive('second',function($rootScope){
    return{
        restrict: 'E',
        template: 'Im second directive! <button ng-click="click()">click me!</button>',
        scope: true,
        link:function(scope,elem,attrs){
            scope.click = function(){
                $rootScope.$broadcast('event','hello!');    
            };

        }
    }    
});

イベントは によって送信され$rootScope.$broadcast('event','hello!');ます。これは、イベントがルート スコープから下位の子スコープに送信されることを意味します。http://jsfiddle.net/aartek/fJs69/

于 2014-07-16T11:17:33.017 に答える