尝试创建AngularJS指令时出现模板错误

时间:2013-11-28 06:13:29

标签: angularjs

我正在尝试创建一个将创建以下HTML的指令,所以我不必在很多地方继续写这个:

    <div class="activity-mask" data-ng-show="loading!=0">
        <span>Loading... {{ loading }}</span>
    </div>
    <div class="activity-mask" data-ng-show="fetching!=0">
        <span>Fetching... {{ fetching }}</span>
    </div>

以下是我的尝试:

 app.directive('myActivity', function() {
    return {
        restrict: "A",
        template: "<div class='activity-mask' data-ng-show='loading!=0'>" +
                   "<span>Loading... {{ loading }}</span>" + 
                   "</div>" +
                   "<div class='activity-mask' data-ng-show='fetching!=0'>" +
                   "<span>Fetching... {{ fetching }}</span>" +
                   "</div>",
        replace: true
    };
});

然而,这给了我以下错误:

Error: [$compile:tplrt] http://errors.angularjs.org/1.2.2/$compile/tplrt?p0=myActivity&p1=
    at Error (<anonymous>)
    at http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:6:449
    at wa (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:51:488)
    at R (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:43:145)
    at R (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:43:241)
    at R (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:43:241)
    at t (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:41:427)
    at h (http://127.0.0.1:81/Scripts/ui-router-master/release/angular-ui-router.min.js:7:11027)
    at l.compile.x (http://127.0.0.1:81/Scripts/ui-router-master/release/angular-ui-router.min.js:7:11814)
    at g.$get.g.$broadcast (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:103:156) angular.min.js:84
(anonymous function) angular.min.js:84
$get angular.min.js:62
$get.g.$broadcast angular.min.js:103
$.transitionTo.$.transition.I.then.$.transition.$.transition angular-ui-router.min.js:7
k.promise.then.A angular.min.js:91
(anonymous function) angular.min.js:93
$get.g.$eval angular.min.js:101
$get.g.$digest angular.min.js:98
$get.g.$apply angular.min.js:101
g angular.min.js:67
w angular.min.js:71
H.onreadystatechange

1 个答案:

答案 0 :(得分:13)

答案在错误堆栈本身。只需按照错误说明链接:

http://docs.angularjs.org/error/$compile:tplrt?p0=myActivity

使用replace: true时,您的模板需要有一个根节点。你的有两个。