页面控制器方法是否可以覆盖指令方法?

Is there a way for a page controller method to override a directive method?

页面控制器可以覆盖指令的行为吗?

所以,

scope.doSomething = function() {
   // whatever in the directive
};

$scope.doSomething = function() {
   // do a different whatever than the directive
};

基本上,该指令对每种情况都有相同的行为,但有一种情况(覆盖),其行为是 "Don't do anything",只是显示。

您的指令应使用其属性为它可能需要的任何其他参数定义接口。

angular.module('theApp', []).directive('someDirective', function () {
    return {
        scope: {
            formattingFn: '=',
        },
        template: "<div> {{viewableData}} </div>",
        link: function(scope){
            // Don't do this in the real world - make sure it quacks first:
            scope.viewableData = (scope.formattingFn || defaultFormat)("Hello World");
        }
    };
    function defaultFormat(data){ 
        return data; 
    }
});

然后将以两种方式之一使用,前者将使用您的函数,后者不会:

<div some-directive formatting-fn="doSomething"></div>
<div some-directive></div>

想法是通过指令的 scope

构建一个接口