在angularjs中重载传递给指令的函数对象或访问实例方法中的方法

override a method in function object or access instance methods which is passed to a directive in angularjs

本文关键字:对象 函数 实例方法 方法 访问 指令 重载 angularjs      更新时间:2023-09-26

需要JavaScript帮助来继承传递给指令的函数对象。这是我的指令函数对象,这个函数被传递给angular.directive

function DDDirective (){
           return {
               restrict: 'EA',
               replace: 'true',
               templateUrl: 'template/abstractLazyLoad.html',
               scope:{
                   id:'@idAttr'
               },
               controller:function($scope){
                   $scope.lazyLoad=funclazyLoadprops();
               }
           }
}
function funclazyLoadprops(){
    return {
        showText: "Show Divisional Directors",
        hideText: "Hide Divisional Directors",
        templateName: 'template/dd/main.html'
    }
}
app.directive('ddDirectiveLazy', DDDirective);
app.directive('ddDirectiveEager',DDDirective);

表示指令

ddDirectiveEager

我想添加一些属性到$scope。懒汉,请告诉我怎么做。我尝试使用JavaScript原型继承,但在控制器函数中,我无法访问DDDirective实例方法。

这几乎不是规定的"有棱角的方式",但这应该能满足您的要求:

function DDDirective (getProps){
    return function(){
        return {
            restrict: 'EA',
            replace: 'true',
            templateUrl: 'template/abstractLazyLoad.html',
            scope:{
                id:'@idAttr'
            },
            controller:function($scope){
                $scope.lazyLoad = getProps();
            }
        };
    };
}
function funclazyLoadprops(){
    return {
        showText: "Show Divisional Directors",
        hideText: "Hide Divisional Directors",
        templateName: 'template/dd/main.html'
    }
}
function funcEagerLoadprops(){
    var lazyprops = funclazyLoadprops();
    //attach additional properties here
    return lazyprops;
}
app.directive('ddDirectiveLazy', DDDirective(funclazyLoadprops));
app.directive('ddDirectiveEager',DDDirective(funcEagerLoadprops));