Angular $ compile不适用于rootScope函数

时间:2017-08-11 15:59:04

标签: javascript angularjs

我在根范围内定义了一个函数:

app.run(
    function($rootScope, $location) {
        // ...

        $rootScope.goToPage = function(view) {
            $location.path("/" + view);
            console.log("go('"+view+"')");
        }

        // ...
    }
);

我在我的观点中使用它:

<a href="" ng-click="goToPage('myPage')">My page</a>

有效。

现在,我正在从REST服务加载一些html部分,我需要编译它们才能执行角度代码和指令。

这是编译它们的指令:

app.directive('initBind', function($compile, $rootScope) {
    return {
        restrict: 'A',
        link : function (scope, element, attr) {
            attr.$observe('ngBindHtml',function(){
                if(attr.ngBindHtml){
                    $compile(element[0].children)(scope);
                }
            })
        }
    };
})

我这样使用它:

<div ng-bind-html="htmlToBind" init-bind>
</div>

注意:htmlToBind已由$sce.trustAsHtml预处理。

htmlToBind内的指令有效,但如果我将a标记与ng-click="goToPage('anotherPage')"放在一起,则无效。我的意思是,页面会刷新而不是执行该功能,并且没有任何内容记录到控制台。

似乎goToPage范围内没有定义$compile,但我也试过传递$rootScope(确实有这个功能)但没有成功。

我错过了什么?

0 个答案:

没有答案
相关问题