动态路由AngularJS [英] dynamic routing AngularJS
问题描述
我试图绑定一个路线
属性的src NG-包括
:
I am trying to bind a route
property to the src for ng-include
:
config.js
$routeProvider.when('/services', {
templatePATH: '/views/services.html'
});
的index.html
<html lang="en" ng-app="myApp" ng-controller="AppController">
<body>
<div ng-include src="{{page}}" class="container"></div>
</body>
</html>
controllers.js
controller('AppController', ['$scope','$route','$routeParams', function($scope, $route, $routeParams) {
var render = function(){
$scope.page = $route.current.templatePATH;
};
$scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
render();
});
}]).
<一个href=\"http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm\"相对=nofollow> 这里就是我的想法。 什么奇怪的是, templateURL
中可以看到其中,的src
从上注入的页面加载DOM NG-包括
指令。然而可悲的是,这是行不通的。
Here's where I got the idea. What is strange is that the value of templateURL
can be seen in the DOM on pageload where src
is injected from the ng-include
directive. Sadly however, this is not working.
是否有可能实现这种约定?
Is it possible to achieve this kind of convention?
推荐答案
噢,你仅仅做一个很小的错误,请更改您的code以下
Oh your are just making a very small mistake please change your code to below
<div ng-include="page" class="container"></div>
更多详细的解答:
当您使用NG-include指令手表放在srcExp这是attr.ngInclude价值|| attr.src所以如果你{{PAGE}}那么你会在页面的不是网页属性值看着那么你必须把页面不插入值,以便它可以页面,而不是网页字符串值上直接观看
More Detailed Answer : When you use ng-include directive a watch is put on srcExp which is value of attr.ngInclude || attr.src so if you do {{page}} then you will be watching on the value of page not on page property so you have to put "page" without interpolated value so that it can watch directly on page instead of page string value
这篇关于动态路由AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!