我应该如何在 Angular ui-router 中创建 templateUrl 属性的路径? [英] how should I create the path of templateUrl property in Angular ui-router?

查看:16
本文介绍了我应该如何在 Angular ui-router 中创建 templateUrl 属性的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙!

我想弄清楚为什么 chrome 控制台返回我找不到我的home.html"模板 GET http://localhost:3000/home.html 404 (Not Found).恐怕我没有设置正确的templateUrl"属性的路径.

I'm trying to figure out why the chrome console returns me that it can't find my "home.html" template GET http://localhost:3000/home.html 404 (Not Found). I'm afraid that I'm not setting correct the path of "templateUrl" property.

这是我的文件夹结构:

app.js
bin/
node_modules/
package.json
public/
---------javascript/
-------------------angularApp.js
routes/
views/
----------index.ejs
----------home.html

这是我的配置代码(位于 angularApp.js 中)

and this is my config code(located in angularApp.js)

angular.module('supervisor', ['ui.router'])
.config([
    '$stateProvider',
    '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl: '/home".html',
                controller: 'MainCtrl'
            });

        $urlRouterProvider.otherwise('/home');
    }
])

我也尝试过不同的路径,比如../../home.html",但没有成功.如果我使用内联模板,它就可以工作(<script type="text/ng-template" id="/home.html">Hello!</script>).

I also tried different paths like "../../home.html" but with no success. If I use inline template it's working(<script type="text/ng-template" id="/home.html">Hello!</script>).

谢谢!

推荐答案

看起来您正在使用 express 来为您的应用程序提供服务.Express 是服务器端框架,而 angular 是客户端框架.您不应该混合使用 2 个代码库.你在 angular 中使用的所有东西都应该作为静态资源.您很可能将公用文件夹作为静态路由提供服务.所以很可能你需要像这样修改你的结构(我将如何构建它):

It would appear you're using express to serve your application. Express is a server side framework, while angular is a client side framework. You shouldn't mix the 2 code bases. Everything you use in angular should be served as static resources. You're very likely serving your public folder as static routes. So most likely you need to modify your structure like so (how I would structure it):

public
--app
----scripts
------angularApp.js
----views
------home.html

那么你的模板网址应该是 '/app/views/home.html'

Then your template url should be '/app/views/home.html'

这篇关于我应该如何在 Angular ui-router 中创建 templateUrl 属性的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆