简体   繁体   English

在Mean.js堆栈上找不到Angular.js指令templateUrl

[英]Angular.js Directive templateUrl not found on Mean.js stack

I've created my first directive in Angular running on the mean.js 0.4-dev stack vertical project file structure which I auto generated with Yeoman. 我在Angular中创建了我的第一个指令,运行在我用Yeoman自动生成的mean.js 0.4-dev堆栈垂直项目文件结构中。

The directive compiles correctly when I use the 'template:' property however when I try to move the contents to a template file and use the 'templateUrl:' property it loads the un-compiled 'layout.server.view.html' file instead of my 'contact-form.client.template.html' file. 当我使用'template:'属性时,该指令正确编译,但是当我尝试将内容移动到模板文件并使用'templateUrl:'属性时,它会加载未编译的'layout.server.view.html'文件我的'contact-form.client.template.html'文件。 It seems like it can't find my template file no matter what I set the path to (I tried all paths up the tree). 无论我设置路径是什么,它似乎无法找到我的模板文件(我尝试了树上的所有路径)。

Can anyone see what I'm doing wrong. 任何人都可以看到我做错了什么。 Perhaps someone can explain how angular resolves relative paths. 也许有人可以解释角度如何解析相对路径。

My program structure is like this... I generated a module for a contact form using the 0.4-dev of the generator-mean. 我的程序结构是这样的...我使用生成器均值的0.4-dev为联系表单生成了一个模块。 The module contains my directive. 该模块包含我的指令。 My file structure is like this: 我的文件结构是这样的:

/app
  /config
  /modules
    /contact-forms
      /client
        /config
        /controllers  
          contact-forms.client.controller.js
        /views
          contact-form.client.template.html <- 2. should load this
        contact-forms.client.module.js
    /core
      /client
        /views
          home.client.view.html <- 1. <contact-form> directive here
      /server
        /controllers
        /routes
        /views
          layout.server.view.html <- 3. instead loads this
    /node_modules
    /public
    /uploads

My directive code is this: 我的指令代码是这样的:

contactForms.directive('contactForm',[function(){
return {
    restrict: 'E',
    transclude: 'true',
    //template: '<div>hello world!</div>', <--this works
    templateUrl: 'modules/contact-forms/client/views/contact-form.client.template.html'
};
}]);

And my template file is like this: 我的模板文件是这样的:

<div>
<form class="row col-md-6 col-md-offset-3 text-left">
    <div class="form-group col-md-12">
        <label for="Name">Name</label>
        <input type="text" class="form-control" id="inputName">
    </div>
    <div class="form-group col-md-12">
        <label for="email">Email</label>
        <input type="email" class="form-control" id="inputEmail">
    </div>
    <div class="form-group col-xs-12">
        <label for="emailSubject">Subject</label>
        <input type="text" class="form-control" id="inputSubject">
    </div>
    <div class="form-group col-xs-12">
        <label for="emailMessage">Message</label>
        <input type="text" class="form-control" id="inputMessage">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

I've seen a few posts on this type of issue however none of them seem to match my use case. 我在这类问题上看到了一些帖子,但它们似乎都与我的用例不符。 I'm running a local express server so I don't think this post is the issue ( Couldn't load template using templateUrl in Angularjs ). 我正在运行本地快递服务器,所以我不认为这篇文章是问题( 无法在Angularjs中使用templateUrl加载模板 )。

This post talks about templateUrl being spelt wrong but I think mine is correct ( Angular directive templateURL not being loaded. Why? ) 这篇文章谈到templateUrl拼写错误,但我认为我的是正确的( Angular指令templateURL没有加载。为什么?

I understand from this post on the angular github ( https://github.com/angular/angular.js/issues/10965 ) that the default behavior of angular.js is to load the index file in the event of not finding a template. 我从这篇关于angular github( https://github.com/angular/angular.js/issues/10965 )的帖子中了解到,angular.js的默认行为是在没有找到模板的情况下加载索引文件。 The comment from feb 4 suggests this is how it has to be. 2月4日的评论表明这是必须的。 There is no errors in my browser console so I'm not really sure whats happening. 我的浏览器控制台没有错误,所以我不确定发生了什么。

My node.js routing hasn't been changed from the Yeoman install of the mean.js stack: 我的node.js路由没有从mean.js堆栈的Yeoman安装中更改:

module.exports = function(app) {
// Root routing
var core = require('../controllers/core.server.controller');

// Define error pages
app.route('/server-error').get(core.renderServerError);
app.route('/not-found').get(core.renderNotFound);

// Define application route
app.route('/*').get(core.renderIndex);
};

and: 和:

'use strict';

/**
 * Render the main application page
 */
exports.renderIndex = function(req, res) {
    res.render('modules/core/server/views/index', {
        user: req.user || null
    });
};

/**
 * Render the server error page
 */
exports.renderServerError = function(req, res) {
    res.status(500).render('modules/core/server/views/500', {
        error: 'Oops! Something went wrong...'
    });
};

/**
 * Render the server not found page
 */
exports.renderNotFound = function(req, res) {
    res.status(404).render('modules/core/server/views/404', {
        url: req.originalUrl
    });
};

Do i need to add a route for the template? 我是否需要为模板添加路线?

I figured out why it's not finding the template. 我弄清楚为什么它找不到模板。 In mean.js 0.4.0 the express configuration file has a static routing function which removes the /client from the paths 在mean.js 0.4.0中,express配置文件具有静态路由功能,该功能从路径中删除/ client

/**
 * Configure the modules static routes
 */
module.exports.initModulesClientRoutes = function (app) {
    // Setting the app router and static folder
    app.use('/', express.static(path.resolve('./public')));

    // Globbing static routing
    config.folders.client.forEach(function (staticPath) {
        app.use(staticPath.replace('/client', ''), express.static(path.resolve('./' + staticPath)));
    });
};

So my line: 所以我的路线:

templateUrl: 'modules/contact-forms/client/views/contact-form.client.template.html'

should be: 应该:

templateUrl: 'modules/contact-forms/views/contact-form.client.template.html'

Which seems to work. 这似乎有效。 No idea why they remove this /client from the path. 不知道为什么他们从路径中删除此/客户端。 There is a reference to it on this issue ( https://github.com/meanjs/mean/issues/608 ) however it doesn't seem to mention exactly why they do it. 在这个问题上有一个参考( https://github.com/meanjs/mean/issues/608 )但是它似乎没有提到他们为什么这样做。

If you want to remove this behavior from mean.js you can do the following (Warning: The yeoman meanjs:vertical-module generator creates all paths without the client so either don't use it or correct its path output after creating each module.): 如果你想从mean.js中删除这个行为,你可以执行以下操作(警告:yeoman meanjs:vertical-module生成器创建没有客户端的所有路径,因此要么在创建每个模块后不使用它或更正其路径输出。 ):

Remove the replace() function in express.js so it looks like this: 删除express.js中的replace()函数,使其如下所示:

/**
 * Configure the modules static routes
 */
module.exports.initModulesClientRoutes = function (app) {
    // Setting the app router and static folder
    app.use('/', express.static(path.resolve('./public')));

    // Globbing static routing
    config.folders.client.forEach(function (staticPath) {
        app.use(staticPath, express.static(path.resolve('./' + staticPath)));
    });
};

For each module in the project add a /client after the module name in every static path. 对于项目中的每个模块,在每个静态路径中的模块名称后面添加/ client。 I did this in a brute force fashion however I'm sure there's a smarter way. 我以蛮力的方式做到了这一点,但我确信这是一种更聪明的方式。

In the config.js file remove the /client masks from lines 121 and 124 so they look like the following: 在config.js文件中,从第121和124行删除/ client掩码,使它们如下所示:

    // Setting Globbed js files
    config.files.client.js = getGlobbedPaths(assets.client.lib.js, 'public/').concat(getGlobbedPaths(assets.client.js, 'public/'));

    // Setting Globbed css files
    config.files.client.css = getGlobbedPaths(assets.client.lib.css, 'public/').concat(getGlobbedPaths(assets.client.css, 'public/'));

Seems to work and doesn't appear to be any issues so far I'll comment if I run into any issues. 似乎工作,似乎没有任何问题到目前为止我会评论,如果我遇到任何问题。

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

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