简体   繁体   English

动态路由AngularJS

[英]dynamic routing AngularJS

I am trying to bind a route property to the src for ng-include : 我正在尝试将ng-includeroute属性绑定到src:

config.js config.js

$routeProvider.when('/services', {
  templatePATH: '/views/services.html'
});

index.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 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();
  });

}]).

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. 令人感到奇怪的是,价值templateURL可以在对页面加载的DOM可以看出src从注入的ng-include指令。 Sadly however, this is not working. 可悲的是,这是行不通的。

Is it possible to achieve this kind of convention? 是否有可能达成这种约定?

Oh your are just making a very small mistake please change your code to below 哦,您只是犯了一个很小的错误,请将您的代码更改为以下内容

<div ng-include="page"  class="container"></div>

More Detailed Answer : When you use ng-include directive a watch is put on srcExp which is value of attr.ngInclude || 更详细的回答:当您使用ng-include指令时,将在atrc.ngInclude的值srcExp上放置一个手表。 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 attr.src,所以如果您执行{{page}},则将监视page的值,而不是page属性,因此必须放置不带插值的“ page”,以便它可以直接在页面上监视而不是在页面字符串值

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

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