简体   繁体   English

AngularJS指令与routeProvider

[英]AngularJS directive vs routeProvider

I have a problem with my directive. 我的指令有问题。 Actually my routeProvider is not being fired when the $location changes my url path. 实际上,当$ location更改我的URL路径时,不会触发我的routeProvider。

Here is my code: 这是我的代码:

var appPath = location.pathname;
var bdaApp = angular.module('bdaApp', ['ui.bootstrap', 'ngResource', 'ngRoute', 
                                       'homeController', 'itemController', 'userController', 
                                       'eildController']);

    bdaApp.config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', { templateUrl : 'layout/home', controller : 'HomeListController'})
            .when('/item', { templateUrl : 'layout/item', controller : 'ItemListController'})
            .when('/user', { templateUrl : 'layout/user', controller : 'UserListController'})

            .when('/eild/consultar', { templateUrl : 'eild/layout', controller : 'EildListController'})
            .when('/eild/detalhes/:bilhetePedido', {templateUrl : 'eild/detalhes', controller : 'EildDetailController'});

    }]);

    bdaApp.directive('myDirective', ['$location', function($location) {
        return {
            restrict: 'A',
            template:
                    '<ul class="nav navbar-nav">' +
                    '   <li ng-repeat="p in parents" class="dropdown">' +
                    '       <a ng-click="go(p.path)" class="dropdown-toggle">' +
                    '           {{p.nome}}' +
                    '           <b ng-show="p.menus.length > 0" class="caret"></b>' +
                    '       </a>' +
                    '       <ul ng-show="p.menus.length > 0" class="dropdown-menu">' +
                    '           <li ng-repeat="s in p.menus">' +
                    '               <a ng-click="go(s.path)">{{s.nome}}</a>' +
                    '           </li>' +
                    '       </ul>' +
                    '   </li>' +
                    '</ul>',
            controller: ['$scope', '$resource', function($scope, $resource) {

                $scope.getItems = function() {
                    $resource(appPath + 'menus/teste').query(function(data) {
                        $scope.parents = data;
                    });
                };
            }],
            link: function(scope, iElement, iAttrs) {
                scope.getItems();
                scope.$watch('parents', function(newVal) {});

                scope.go = function(path) {
                    if (path != null) {
                        $location.path(path);
                    }
                };
                scope.$watch('go', function(newVal) {});
            }
        };
    }]);

and my html 和我的HTML

<div>
    <div my-directive items="items"></div>
</div>

when I click on any link, it calls my "go" function, change my url to " http://localhost:8080/bda-web/#/item ", but does not change the page. 当我单击任何链接时,它将调用我的“ go”函数,将我的URL更改为“ http://localhost:8080/bda-web/#/item ”,但不会更改页面。 So I assume my routeProvider is not being called through my directive. 因此,我假设未通过我的指令调用我的routeProvider。

I also tried to externalize my function like 我也试图将我的功能外部化

bdaApp.controller('MenuController', ['$scope', '$location', function($scope, $location) {
    $scope.go = function(path) {
        if (path != null) {
            alert(path);
            $location.path(path);
        }
    };
}]);

but I couldn't do this work. 但是我做不到

Anyway, any suggestion? 无论如何,有什么建议吗?

Thanks 谢谢

我只是使用<a href>而不是<a ng-click>进行了变通

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

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