为了好玩,我创建了一个指令来创建标签样式导航。这是指令:
define(['angular'], function (ng) {
var directives = ng.module('directives', []);
/* top navigation tabs */
directives.directive('tabset', function () {
return {
restrict: 'E',
scope: {},
transclude: true,
replace: true,
templateUrl: 'common/components/directives/templates/tabset.tpl.html'
};
})
.directive('tab', ['$location', function ($location) {
return {
restrict: 'E',
replace: true,
scope: {
route: '@',
title: '@'
},
link: function (scope, element, attrs) {
var route = attrs.route;
scope.location = $location;
scope.active = false;
scope.$watch('location.path()', function (new_route) {
scope.active = route === new_route;
});
},
templateUrl: 'common/components/directives/templates/tab.tpl.html'
};
}]);
return directives;
});
Angular 不断抛出这个错误:
TypeError: undefined is not a function
at new ngDirective.controller (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:14357:5)
at invoke (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:3000:28)
at Object.instantiate (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:3012:23)
at http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4981:24
at http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4560:17
at forEach (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:137:20)
at nodeLinkFn (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4545:11)
at compositeLinkFn (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4191:15)
at compositeLinkFn (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4194:13)
at compositeLinkFn (http://localhost:8080/uwr-tournament-handler/build/common/vendor/angular/angular.js:4194:13)
我不知道为什么。我没有看到任何undefined
我尝试将其用作功能的东西。任何人都可以帮忙吗?
该错误是不规则的,但大多数情况下都会抛出该错误。也许它与异步加载有关?它似乎只在页面加载时发生,而不是在浏览网站时发生。
我在后台使用咕噜声。我也使用requirejs。
除此之外,它似乎可以正常工作。
tab.tpl.html:
<li data-ng-class="{active: active}">
<a href="#{{route}}">{{title}}</a>
</li>
tabset.tpl.html:
<ul class="nav nav-tabs" ng-transclude></ul>
用法:
<tabset>
<tab data-route="/home" data-title="Home"></tab>
<tab data-route="/tournaments" data-title="Tournaments"></tab>
<tab data-route="/about" data-title="About"></tab>
</tabset>