javascript - 如何使用 angular.js 创建事件(类)

标签 javascript angularjs

我正在使用[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/

相关文章:

javascript - 手机自动高度虚拟列表显示大列表

javascript - 谷歌分析片段应该在 <head> 里面吗

javascript - SignalR:调用集线器方法时发生内部服务器错误

javascript - Redux 的问题 - 从状态添加和删除项目

javascript - 将表达式而不是结果传递给指令

javascript - 访问 promise 之外的 AngularJS 工厂数据

javascript - 循环执行获取请求

javascript - ng-disabled 也不启用提交按钮

javascript - 数组更改时不调用 AngularJs Formatter

angularjs - 指令的 bool 范围值