【问题标题】:Angular Directive template throw an errorAngular 指令模板抛出错误
【发布时间】:2017-08-21 10:23:24
【问题描述】:

我在 Angular 指令模板中遇到了一些问题。

'use strict';

 function HomeDirective() {
  return {
    controller: 'homeController',
    template: require('./views/home.html'),

    restrict: 'AEC',
    scope: true
  }
}

module.exports = HomeDirective;

我在视图文件夹中有一个模板。我的视图是

<div>
<p>This is Home Page.You can nevigate to any page</p>
<p>The Content is {{test}}</p>
</div>

错误如下

<div>                                                                           
  ^                                                                               
ParseError: Unexpected token                                                        
at wrapWithPluginError (C:\Users\DAT-Asset-110\Desktop\Test 
Code\anguler_test_structure\node_modules\gulp-browserify\index.js:44:10) 

当我使用它时它正在工作

'use strict';

 function HomeDirective() {
    return {
    controller: 'homeController',

   template: '<div><p>This is Home Page.You can nevigate to any page</p>
   <p>The Content is {{test}}</p></div>',
    restrict: 'AEC',
    scope: true
  }
 }

 module.exports = HomeDirective;

谁能帮帮我。问题是什么,解决方法是什么?

【问题讨论】:

  • 你在使用 webpack 来加载你的模板吗? .如果不是,那么它应该是 templateUrl 而不是 template 。
  • 能否发一下使用webpack的正确链接步骤。我用的是browserify。@ShushanthPallegar

标签: javascript jquery angularjs node.js


【解决方案1】:

使用 templateUrl 代替模板:templateUrl: 'views/home.html'

'use strict';

 function HomeDirective() {
  return {
    controller: 'homeController',
    templateUrl: 'views/home.html',

    restrict: 'AEC',
    scope: true
  }
}

module.exports = HomeDirective;

【讨论】:

  • 感谢您的回答。当我使用模板而不是 templateUrl @Syed 时出现什么问题
  • require('./views/home.html'), 一个返回 html 的函数吗?
猜你喜欢
  • 2017-08-24
  • 2012-11-21
  • 2015-04-28
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
相关资源
最近更新 更多