【问题标题】:template string with ng-if has Error: $parse:syntax带有 ng-if 的模板字符串有错误:$parse:syntax
【发布时间】:2019-08-30 13:36:03
【问题描述】:

我有一个字符串

const notes ="some notes"

下面是笔记模板字符串

const notesTemplate = `<span ng-if="${notes} !=''">${notes}</span>`

在上面的语句中我得到了错误

angular.js:12609 错误:$parse:syntax http://errors.angularjs.org/1.4.8/$parse/syntax?p0=vdsdsf&p1=is%20an%20unexpected%20token&p2=4&p3=vf%20vdsdsf%20!%3D%3D%27%27&p4=vdsdsf%20!%3D%3D

我不明白我哪里出错了,任何帮助将不胜感激

【问题讨论】:

  • 你到底想在这里做什么?您是否尝试将其绑定到模板? ng-if 在动态添加到模板时不会呈现。

标签: javascript angularjs


【解决方案1】:

当您在模板字符串中引入变量时,它需要用引号引起来,请遵循error URL,它会使事情变得更加清晰。您的模板字符串可能如下:

const notesTemplate = `<span ng-if= "'${notes}' != ''">${notes}</span>`;

【讨论】:

    【解决方案2】:

    您可以使用以下代码。

    const notesTemplate = <span  ng-if="notes">${notes}</span>
    

    希望对你有所帮助。

    【讨论】:

    • 他使用的是 angularJS 版本 1.4.8,而不是 Angular >= 2。它也是 *ngIf 和大写的 'i'
    • 检查我的更新答案@StefanRein。而在之前的答案中,如果是 *ngIf 中的小写 i 的错误。
    • 这有帮助吗? @StefanRein
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多