Angularjs 角度指令函数调用ng更改

Angularjs 角度指令函数调用ng更改,angularjs,angularjs-directive,angularjs-bootstrap,Angularjs,Angularjs Directive,Angularjs Bootstrap,我有一个角度引导模式弹出框,其中包含模板中的自定义指令: Modal.html 我从主控制器打开模式弹出窗口,如下所示: MainCtrl.js 从上面的代码中可以看到,模式模板包含我的自定义指令,它将$scope.test-定义在ModalCtrl-中,作为它的模型,并且当模型更改时,应该调用$scope.doSomething() 自定义指令如下所示: MyDirective.js angular.module('myApp').directive('myDirective',functio

我有一个角度引导模式弹出框,其中包含模板中的自定义指令:

Modal.html 我从主控制器打开模式弹出窗口,如下所示:

MainCtrl.js 从上面的代码中可以看到,模式模板包含我的自定义指令
,它将
$scope.test
-定义在
ModalCtrl
-中,作为它的模型,并且当模型更改时,应该调用
$scope.doSomething()

自定义指令如下所示:

MyDirective.js
angular.module('myApp').directive('myDirective',function(){
返回{
控制器:功能($scope){
$scope.doSomething=函数(){
log('doSomething()调用了!$scope.test now='+$scope.test);
};
},
链接:功能($scope,el,att){
log('$scope.test='+$scope.test);
},
模板:“”
};
} );
当模式弹出窗口打开时,控制台打印出
$scope.test=empty
,这正是我希望看到的。但是当调用
ModalCtrl
中的
$scope.upload()
函数更改
$scope.test
时,不会发生任何事情!我认为应该发生的是
$scope.doSomething()
被调用,然后应该在控制台中打印出来
doSomething()被调用$scope.test now=更改


任何帮助都将不胜感激,这让我发疯

我明白了。我没有使用ng模型和ng更改,而是添加了:

scope: {
    test: '@'
}
添加到我的指令中,并将其添加到我的链接函数中:

$scope.$watch( 'test', function() { 
    console.log( 'test changed' ); 
} );
然后,我将test作为参数添加到我的directives标记中,如下所示:

<my-directive test="test" />


最后,每当我更改$scope.test时,我调用
$scope.$apply()
,瞧

ng change
的文档中可以看到:“当用户更改输入时,请评估给定的表达式。”此元素中未发生任何用户更改(它是一个div,它无论如何都不理解更改),因此不会触发任何事件!
angular.module( 'myApp' ).directive( 'myDirective', function() {
    return {
        controller: function( $scope ) {
            $scope.doSomething = function() {
                console.log( 'doSomething() called! $scope.test now = ' + $scope.test );
            };
        },

        link: function( $scope, el, att ) {
            console.log( '$scope.test = ' + $scope.test );
        },

        template: '<div class="thumbnail"></div>'
    };
} );
scope: {
    test: '@'
}
$scope.$watch( 'test', function() { 
    console.log( 'test changed' ); 
} );
<my-directive test="test" />