简体   繁体   English

如何使用angular.js创建活动(类)

[英]How do I create an active (class) using angular.js

I am using [uikit] and like to utilize their active class on a link. 我正在使用[uikit] ,喜欢在链接上利用其活动类。 I'd also like to use their icons simultaneously. 我也想同时使用它们的图标。 1 I am trying to implement this practice, fiddle however I get this error. 1我想实现这种做法, 小提琴 ,但是我得到这个错误。

Uncaught SyntaxError: Unexpected token .

and

`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.`

This is my HTML: 这是我的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>

// create the module and name it rustyApp //创建模块并将其命名为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'
    ];

});

// route for the home page //主页的路由

 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) {};

I suspect I am not hooking the .directive correctly or not including a controller but if you look at the fiddle there isn't one! 我怀疑我没有正确连接.directive或不包含控制器,但是如果您看一眼小提琴,那就没有了!

UPDATE: I wound with the following: 更新:我伤了以下:

HTML 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 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');
            }
        });
    }
};

}); });

You just need to change .directive(... to rustyApp.directive(... and take it outside of the config function. 您只需要将.directive(...更改为rustyApp.directive(... ,并将其置于config函数之外。

This line causes the first JS error which causes rustyApp to fail to load as you have it inside the config for some reason. 这行代码会导致第一个JS错误,由于某些原因,它会导致rustyApp无法加载,因为您将其放入配置文件中。

I think you've misunderstood what's going on so I'll just write up this little example here: ``` rustyApp = angular.module(...).controller(...); 我认为您误解了正在发生的事情,所以我在这里只写这个小例子:

// Same result as below. //与以下结果相同。 IMO you should use the below generally as it is clearer. 海事组织,您应该通常使用以下内容,因为它更清晰。

rustyApp = angular.module(...); rustyApp = angular.module(...); rustyApp.controller(...); rustyApp.controller(...); ``` ```

Also directive , config , run , factory , service , controller (and any I've forgotten) are all functions that are available on a module 'object'. 同样, directiveconfigrunfactoryservicecontroller (以及我遗忘的任何东西)都是模块“ object”上可用的所有功能。 When you call one of them the result of the function is the module you called it on. 当您调用其中之一时,函数的结果就是您调用过的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM