简体   繁体   English

angularjs ng-template单​​独的文件没有webserver

[英]angularjs ng-template separate file no webserver

So I'm trying to create an app that needs no webserver with AngularJS 所以我正在尝试使用AngularJS创建一个不需要Web服务器的应用程序

I'm using <script type="text/ng-template" id="loginView.html" src="views/login.html"></script> tags to use separate templates and keep things clean. 我正在使用<script type="text/ng-template" id="loginView.html" src="views/login.html"></script>标签来使用单独的模板并保持清洁。

I wire the thing up in: 把电线连接起来:

app.config(function($routeProvider, $locationProvider){
    $locationProvider.hashPrefix("!");
    $locationProvider.html5Mode(false);

    $routeProvider
        .when("/", {
            templateUrl: "loginView.html",
            controller: "LoginCtrl"
        })
        .when("/main", {
            template: "main",
            controller:"MainCtrl.html"
        })
        .otherwise({
            redirectTo: "/"
        });
});

My files are not loading. 我的文件没有加载。 If I put context inside the script tag I can see it. 如果我将上下文放在脚本标记内,我可以看到它。 I have an ng-view to contains the views. 我有一个包含视图的ng-view。 But I can't seem to get content from separate files. 但我似乎无法从单独的文件中获取内容。 No errors are given. 没有错误。 Is this possible without a webserver? 如果没有网络服务器,这可能吗?

Try using <div ng-view /> instead of your script. 尝试使用<div ng-view />而不是脚本。 You can fetch the template just from your file system, you don't need to be on a web server. 您只需从文件系统中获取模板,就不需要在Web服务器上。

Where are you putting those script tags? 你把这些脚本标签放在哪里? In the HTML? 在HTML中? I don't even know that a script tag like that will do what you expect it to, angular or otherwise, but it isn't necessary. 我甚至不知道像这样的脚本标签会按照您的预期,角度或其他方式执行,但这不是必需的。 The call you make to $routeProvider.when is sufficient to load the HTML templates. 您对$routeProvider.when的调用足以加载HTML模板。 You WILL have to reverse the parameters for your /main route though, as you seem to have the template / templateUrl and controller reversed. 您将不得不反转/main路径的参数,因为您似乎已将template / templateUrlcontroller反转。 Use templateUrl there though. 尽管使用templateUrl template is for raw HTML. template用于原始HTML。

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

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