Javascript 无服务器的带Angularjs的玉模板
我知道客户端模板呈现是一个有争议的话题,但当编码以与所有web服务器兼容时,这是必要的 我在寻找一个使用angularjs渲染jade模板的简单解决方案。。。 下面是一个过于简化的示例: 该示例呈现单个页面和引导以评估元素。我想要的是一个解决方案,它使用路由调用jade文件,渲染它们,然后使用angularjs进行评估 是否有实现此功能的方法或解决方法Javascript 无服务器的带Angularjs的玉模板,javascript,angularjs,pug,Javascript,Angularjs,Pug,我知道客户端模板呈现是一个有争议的话题,但当编码以与所有web服务器兼容时,这是必要的 我在寻找一个使用angularjs渲染jade模板的简单解决方案。。。 下面是一个过于简化的示例: 该示例呈现单个页面和引导以评估元素。我想要的是一个解决方案,它使用路由调用jade文件,渲染它们,然后使用angularjs进行评估 是否有实现此功能的方法或解决方法 请注意,模板的呈现需要无服务器(即无节点)。这里有一个解决方案,您可以将所有模板嵌入angularng template脚本标记中: <s
请注意,模板的呈现需要无服务器(即无节点)。这里有一个解决方案,您可以将所有模板嵌入angular
ng template
脚本标记中:
<script type="text/ng-template" id="myTemplate.html" class="angular-template">
<!-- jade template --></script>
使用ng view
进行路由的示例应用程序:
var app = angular.module('plunker', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'myTemplate.html' /* note is same as script tag ID above*/
});
});
app.controller('testApp', function($scope){
$scope.title="See script tag for template";
$scope.numbers = [1,2,3,4,5]
})
angular.bootstrap(document, ['plunker'])
有争议,为什么?所有内容都在一页中的解决方案对您有效吗?模板可能都在脚本标记中。应用程序已经使用了单个页面(与示例非常相似)。我需要渲染模板,并让angular评估和使用ng属性。我应该能够编译它们并将它们推入
$templateCache
。如果templateUrl
存在于$templateCache
中,则首先从那里提取它以保存不需要的ajax调用。使用脚本标记。或者,在引导angularOk之前,用jade编译的html替换所有脚本标记innerhtml。我理解其中的逻辑。我不是在寻找一个复制品,但如果你能用一些示例代码来回答,这将是最有帮助的,因为我只是从angular开始。谢谢。真的所有这些都是angular内置的惊人功能。。。。我只是帮你解释一下
var templates = document.querySelectorAll('.angular-template')
for (i = 0; i < templates.length; i++) {
templates[i].innerHTML = jade.compile(templates[i].innerHTML.trim())()
}
var app = angular.module('plunker', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'myTemplate.html' /* note is same as script tag ID above*/
});
});
app.controller('testApp', function($scope){
$scope.title="See script tag for template";
$scope.numbers = [1,2,3,4,5]
})
angular.bootstrap(document, ['plunker'])