0
我的指令有问题。实际上,当$ location改变我的url路径时,我的routeProvider没有被触发。AngularJS指令vs routeProvider
这里是我的代码:
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) {});
}
};
}]);
和我的HTML
<div>
<div my-directive items="items"></div>
</div>
当我点击任何链接,它会调用我的 “走出去” 的功能,改变我的网址为 “http://localhost:8080/bda-web/#/item
”,但不会更改页面。所以我假设我的routeProvider没有被我的指令调用。
我也试图外部化我之类的函数
bdaApp.controller('MenuController', ['$scope', '$location', function($scope, $location) {
$scope.go = function(path) {
if (path != null) {
alert(path);
$location.path(path);
}
};
}]);
,但我不能做这个工作。
无论如何,有什么建议吗?
感谢
所以'path'你去功能是否正确等同了呢?你有没有尝试在'$ apply'中包装'$ location.path(path)'?尝试改变你的去功能:http://pastie.org/8676192 – Ronnie
嗨@Ronnie我收到“/”或“/ item”在我的路径,这意味着,我的整个网址更改为http:// localhost:8080/BDA的Web /#/项。 我尝试申请,但它说我的$ rootScope进行中 –
hmm,尝试添加默认的'.otherwise({redirectTo:'/ item'});'看看它是否默认为'/ item'页面。我不明白为什么这不起作用。我刚刚开始使用几个星期前的路由,并且我从控制器执行'$ location.path()'调用,而不是指令 – Ronnie