javascript 语法错误:令牌“mod”是意外的,期望在表达式 [partials/{{mod}}.html] 的第 12 列处出现 [:],从 [mod}}.html] 开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17533973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:41:28  来源:igfitidea点击:

Syntax Error: Token 'mod' is unexpected, expecting [:] at column 12 of the expression [partials/{{mod}}.html] starting at [mod}}.html]

javascriptangularjs

提问by zajca

I'm trying to do simple think. use ng-include in loop for including multiple html pages to rendered page.

我正在尝试做简单的思考。在循环中使用 ng-include 将多个 html 页面包含到呈现的页面中。

<div ng-controller="pageCTRL">
 <div ng-repeat="mod in modules">
  <div ng-include src='partials/{{mod}}.html'></div>
 </div>
</div>

$scope.modules = ["mod_nav","mod_feature","mod_footer"];

But all i get is this.

但我得到的只是这个。

Error: Syntax Error: Token 'mod' is unexpected, expecting [:] at column 12 of the expression [partials/{{mod}}.html] starting at [mod}}.html].
at Error (<anonymous>)
at throwError (angular.js:6066:11)
at consume (angular.js:6104:7)
at object (angular.js:6394:9)
at primary (angular.js:6278:17)
at unary (angular.js:6265:14)
at multiplicative (angular.js:6251:39)
at additive (angular.js:6239:16)
at relational (angular.js:6230:16)
at equality (angular.js:6221:16) <div ng-include="" src="partials/{{mod}}.html"> 

Do you have some solution how to do it?

你有什么解决办法吗?

rendered code looks like this:

呈现的代码如下所示:

  <div ng-include="" src="partials/mod_nav.html"></div>

so I don't see what I'm doing wrong

所以我看不出我做错了什么

回答by Ven

ng-include's srcexpects an expression, not a string :

ng-include'ssrc需要一个表达式,而不是一个字符串:

<div ng-include src="'partials/' + mod + '.html'"></div>