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

时间:2014-10-06 00:40:04

标签: javascript angularjs

我正在使用[uikit]并喜欢在链接上使用他们的活动课程。我也想同时使用他们的图标。 1我正在尝试实施这种做法,fiddle但是我收到了这个错误。

Uncaught SyntaxError: Unexpected token .

`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或者没有包括控制器,但是如果你看一下这个小提琴,那就不是了!

更新 我伤痕累累:

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

});

1 个答案:

答案 0 :(得分:1)

您只需将.directive(...更改为rustyApp.directive(...,然后将其置于配置功能之外。

这一行会导致第一个JS错误,导致rustyApp因为某些原因而无法加载,因为你在配置中有错误。

我认为你已经误解了发生了什么,所以我在这里写下这个小例子: ``` rustyApp = angular.module(...)。controller(...);

//结果如下。国际海事组织你应该使用以下一般,因为它更清楚。

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

此外directiveconfigrunfactoryservicecontroller(以及任何我已经忘记的)都是模块&#39;对象&#39;上可用的所有功能。当你调用其中一个时,函数的结果就是你调用它的模块。

相关问题