我正在使用[uikit]并喜欢在链接上使用他们的活跃类别。我还想同时使用他们的图标。 1我正在尝试实现这种做法,fiddle但是我收到这个错误。
未捕获的语法错误:意外的标记。
和
`Uncaught Error: [$injector:modulerr]
Failed to instantiate module rustyApp due to:
Error: [$injector:nomod] Module 'rustyApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.`
这是我的 HTML:
<section class="top-bar-section uk-navbar-flip" ng-app="link">
<ul class="uk-navbar-nav ">
<li active-link="uk-active"><a href="/#home"><i class="uk-icon-home uk-icon-medium "> </i>home</a></li>
<li active-link="uk-active"><a href="/#work"><i class="uk-icon-photo uk-icon-medium "></i> work</a></li>
<li active-link="uk-active"><a href="/#contact"><i class="uk-icon-envelope-o uk-icon-medium "></i> contact</a></li>
</ul>
</section>
//创建模块并将其命名为 rustyApp
var rustyApp = angular.module('rustyApp', [
'ngRoute',
'viewController',
'mm.foundation',
'angular-flexslider'
])
.controller('BasicSliderCtrl', function($scope) {
$scope.slides = [
'../images/sliderContent/1.jpg',
'../images/sliderContent/2.jpg',
'../images/sliderContent/3.jpg',
'../images/sliderContent/4.jpg'
];
});
//主页路由
rustyApp.config(function($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeController'
})
.when('/work', {
templateUrl: 'partials/work.html',
controller: 'WorkController'
})
.when('/contact', {
templateUrl: 'partials/contact.html',
controller: 'ContactController'
})
.otherwise({
redirectTo: '/home'
});
});
var viewController = angular.module('viewController', []);
var viewController = angular.module('viewController', []);
.directive('activeLink', ['$location', function(location) {
return {
restrict: 'A',
link: function(scope, element, attrs, controller) {
var clazz = attrs.activeLink;
var path = attrs.href;
path = path.substring(1); //hack because path does bot return including hashbang
scope.location = location;
scope.$watch('location.path()', function(newPath) {
if (path === newPath) {
element.addClass(clazz);
} else {
element.removeClass(clazz);
}
});
}
};
}]);
rustyApp.controller('HomeController', function($scope) {});
rustyApp.controller('WorkController', function($scope) {});
rustyApp.controller('ContactController', function($scope) {});
var OffCanvasDemoCtrl = function($scope) {};
我怀疑我没有正确连接.directive
或者没有包含 Controller ,但是如果你看一下 fiddle ,就发现没有 Controller !
更新: 我的伤口如下:
HTML
<section class="top-bar-section uk-navbar-flip">
<ul class="uk-navbar-nav ">
<li my-active-link="/"><a href="/#home"><i class="uk-icon-home uk-icon-medium "> </i>home</a></li>
<li my-active-link="/work"><a href="/#work"><i class="uk-icon-photo uk-icon-medium "></i> work</a></li>
<li my-active-link="/contact"><a href="/#contact"><i class="uk-icon-envelope-o uk-icon-medium "></i> contact</a></li>
</ul>
</section>
JS
rustyApp.directive('myActiveLink', function($location) {
return {
restrict: 'A',
scope: {
path: "@myActiveLink"
},
link: function(scope, element, attributes) {
scope.$on('$locationChangeSuccess', function() {
if ($location.path() === scope.path) {
element.addClass('uk-active');
} else {
element.removeClass('uk-active');
}
});
}
};
});
最佳答案
您只需将 .directive(...
更改为 rustyApp.directive(...
并将其放在配置函数之外。
此行会导致第一个 JS 错误,导致 rustyApp 无法加载,因为由于某种原因您将其包含在配置中。
我认为你误解了发生的事情,所以我在这里写下这个小例子: ```` rustyApp = angular.module(...).controller(...);
//与下面相同的结果。在我看来,您通常应该使用下面的内容,因为它更清楚。
rustyApp = angular.module(...); rustyApp.controller(...); ````
还有指令
、配置
、运行
、工厂
、服务
、controller
(以及我忘记的任何一个)都是模块“对象”上可用的函数。当您调用其中之一时,该函数的结果就是您调用它的模块。
关于javascript - 如何使用 angular.js 创建事件(类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26208700/