【问题标题】:Load some functions after successful render of custom directive成功渲染自定义指令后加载一些函数
【发布时间】:2015-07-23 13:01:23
【问题描述】:

我有一个指令如下所示:

angular.module("myApp")
.directive('formProgress', function () {
    return {
        restrict: 'E',
        scope: {
            headingfirst: "@",
            headinghighlight: "@",
            values: "=",
            numwords: "=",
            customcallback: "&"
        },
        templateUrl: 'partial-htmls/toolbox/form-progress.html',
        link: function(scope, elm, attrs){
            /* this is rendering before the template is rederred */
            /* but it should render after directive has rendered right? */
            console.dir(arguments);
            commonUtilities.navigateToForm(3);
        }
    };
});

我正在尝试在成功加载自定义指令后调用回调函数。

我尝试使用link 属性,如link 中所述。

甚至尝试在$scope 对象中定义一个loadcallback 函数并在自定义指令中调用它:

$scope.loadcallback = function(){
    //Here your view content is fully loaded !!
    commonUtilities.navigateToForm($state.current.formIndex);
}

然后在html中:

<form-progress customcallback="loadcallback()">
</form-progress>

但这没有用。我什至在控制器中尝试了以下操作,但也不起作用。

$scope.$on('$viewContentLoaded', function(){
    //Here your view content is fully loaded !!
    commonUtilities.navigateToForm($state.current.formIndex);
});

【问题讨论】:

    标签: javascript jquery angularjs angularjs-directive


    【解决方案1】:

    您可以从指令本身调用回调。
    只需将其传递给指令的控制器即可:

    scope: {
       customcallback: "&"
    },
    controller: function(scope, elm, attrs){
        scope.customcallback();
    }
    

    HTML:

    <form-progress customcallback="loadcallback"></form-progress>
    

    【讨论】:

    • 使用 '&' 而不是 '='
    • @ngLover 我也尝试过使用&amp;。这仍然不会调用自定义指令的完整加载。
    • @Mr_Green:它确实在指令的完整 load 时调用它,但这与 render不同>.
    • @Cerbrus 哦,那我实际上是想在完成渲染时回调一个函数。请原谅我糟糕的英语。
    【解决方案2】:

    这可以帮助您满足您的要求。

    angular.module("myApp")
    .directive('formProgress', function () {
        return {
            restrict: 'E',
            scope: {
                headingfirst: "@",
                headinghighlight: "@",
                values: "=",
                numwords: "=",
                customcallback: "&"
            },
            templateUrl: 'partial-htmls/toolbox/form-progress.html',
            link: function(scope, elm, attrs){
                /* this is rendering before the template is rederred */
                /* but it should render after directive has rendered right? */
                console.dir(arguments);
                commonUtilities.navigateToForm(3);
              elm.ready(function(){
                scope.customcallback();
              })
            }
        };
    });
    

    【讨论】:

    • 在自定义指令渲染后仍然不会调用该函数。 (之前打电话)
    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多