两个自定义指令交互时,第一个指令scope上的值变化时,第二个指令上某个函数执行 怎么操作?

weChatDirective.directive('tab1', function() {
    return {
        restrict: 'E',
        replace : true,
        templateUrl: '..',
        controller : function($scope){
            $scope.viewdata = {    
                   "type":"view",
                   "name":"外部连接",
                   "url":"http://www.soso.com/"
            };
            this.viewdata = $scope.viewdata;
            
        },
        link: ...
    };
});

weChatDirective.directive('tabReply', function() {
    return {
        restrict: 'E',
        require : '^tab1', 
        controller : ["$scope",function($scope){
            $scope.hasChanged = function(){};
        }],
        replace: true,
        templateUrl: './dist/weChat/templates/weChatMenuReply.html',
        link: function(scope, element, attr,reController) { 
            console.log(reController);
        }
    }
})

tab1指令中的viewdata变化的时候 想要执行tabReply指令里的hasChanged函数 如何实现??大神赐教。还是我陷入思维误区了。。。。

阅读 3.1k
2 个回答

在tabReply指令的controller中加上一行

    $scope.$watch('viewdata',$scope.hasChanged)

没这样尝试过,如果2个指令需要通信时,可以使用事件$emit,$on来进行。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题