2014-11-05 45 views
0

我正在使用templateUrl在我的网页中显示特定的PHP页面。现在我想要废除各个php页面并显示传递给它的变量的代码。回到这个最简单的方法是什么?从单个模板移动到内嵌

var AppModule = angular.module('App', ['ngAnimate', 'ngRoute']); 

AppModule.config(function($routeProvider) { 
    $routeProvider 

     .when('/page:pageNumber', { 
      templateUrl: function ($routeParams) { 
       return '/app/..../assets/html/page' + $routeParams.pageNumber + '.php'; 
      }, 
      controller: "PageCtrl" 
     }) 
     .otherwise({ 
      redirectTo: "/page1" 
     }); 
}); 

AppModule.controller("ViewCtrl", function($scope, $timeout) { 
    $scope.$on("$routeChangeSuccess", function(event, current, previous) { 

     ...stuff... 
    }); 
}); 

回答

1

通过text/ng-template使用脚本,可以让你写你的模板内联,而宣称的URL通过访问它们。下面的代码可以直接在你的index.html中,如果你的配置设置为显示'/my-template.html',那么内联模板将会在它上面的ng-view中输出。

<ng-view /> 
<script type="text/ng-template" id="/my-template.html"> 
    template goes here 
</script> 

然后在你的配置:

.when('/', { 
    templateUrl: '/my-template.html' 
}); 

下面是从角文档多一点信息: https://docs.angularjs.org/api/ng/directive/script

最后,这种技术被证明的TodoMVC例子为角一个:

查看:https://github.com/tastejs/todomvc/blob/gh-pages/examples/angularjs/index.html

Config:https://github.com/tastejs/todomvc/blob/gh-pages/examples/angularjs/js/app.js