【问题标题】:Angular 1 - Compile ng-if condition into stringAngular 1 - 将 ng-if 条件编译成字符串
【发布时间】:2018-04-25 23:30:01
【问题描述】:

我有这个字符串:

this.badge = `<span class="badge">{{ notification}}</span>`

解释我所做的 {{ notification}} 表达式:

this.badge = this.$interpolate(this.badge)(this)

我现在想做的是在span 元素中添加一个ng-if

this.badge = `<span ng-if="notification > 0"class="badge">{{ notification}}</span>`

但是$interpolate 服务不支持这个,我如何“编译” ng-if 的条件?

【问题讨论】:

    标签: javascript angularjs angularjs-compile angularjs-interpolate


    【解决方案1】:

    你可以使用本地scope:

    this.badge = '<span ng-if="'+(scope.notification > 0)+'" class="badge">'+scope.notification+'</span>';
    

    【讨论】:

    • .setAttribute("ng-if", "notification > 0");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多