javascript 加载指令时调用 init 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33342437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:22:54  来源:igfitidea点击:

Calling a init function when a directive is loaded

javascripthtmlangularjshtml-parsingangularjs-ng-init

提问by Itsik Mauyhas

I have a simple directive that loads comments from a service(commentsService):

我有一个从 service( commentsService)加载注释的简单指令:

'use strict';

angular.module('mean.rank')
    .directive('commentList', function(CommentsService, Global) {
        return {
            restrict: 'E',
            templateUrl:  'rank/views/comments-list.html',
            replace: false,
            link: function($scope, element, attrs) {
                //accessing the ad info :  console.log("ADD  " , $scope.ad);
                $scope.comments = [];

                $scope.loadComments = function () {
                    console.log("in loadComments");
                    var adID = {};
                    adID.ad_ID = $scope.ad.nid;
                    console.log("calling services.getComments function with  " , adID.ad_ID);
                    CommentsService.getComments(adID.ad_ID)
                        .then(function (response) {
                            angular.forEach(comment in response)
                            $scope.comments.push(comment);
                        });


                };


            }
        }
    })

The loaded comments should be loaded to the list(inside the templateUrl) using ng-initand then the service for loading (I will add the code if needed).

加载的注释应加载到列表(在 内templateUrlng-init,然后使用加载服务(如果需要,我将添加代码)。

<div ng-init="loadComments()">
    <ul class="media-list comment-detail-list" >
        <li class="media" ng-repeat="comment in comments" >
            <article>
                <div class="pull-left comment-info">
                    <a href="#" class="author">{{ comment.author }}</a><br />
                    <time>{{ comment.datePublished | date:"MM/dd/yyyy" }}</time>
                </div>
                <div class="media-body">
                    <div class="comment-body">
                        <p>{{ comment.comment }}</p>
                    </div>
                </div>
            </article>
        </li>
        <li>Debugger</li>
    </ul>
</div>

The directive has in its scope the loadCommets()function but it is not triggered. Thank's for your help!

该指令在其范围内具有该loadCommets()功能,但不会被触发。谢谢你的帮助!

采纳答案by Icycool

I'd suggest putting the function call inside the link function itself instead of ng-init.

我建议将函数调用放在链接函数本身而不是 ng-init 中。

angular.module('mean.rank')
    .directive('commentList', function(CommentsService, Global) {
        return {
            ...
            link: function($scope, element, attrs) {
                //accessing the ad info :  console.log("ADD  " , $scope.ad);
                $scope.comments = [];

                $scope.loadComments = function () {
                    ...
                };

                $scope.loadComments();
            }
        }
    })

Edit: by the way your forEach syntax is wrong. It should be

编辑:顺便说一下,您的 forEach 语法是错误的。它应该是

angular.forEach(response, function(comment){
    $scope.comments.push(comment);
});