如何在AngularJS中向{{variable.value}}添加条件?

时间:2017-03-27 00:10:12

标签: javascript jquery angularjs json conditional-statements

如果条件为{{variable.value}}

,我想打印一个值

我不知道怎么做,我正在使用div:

<div ng-if="true">{{variable.value}}</div>

但我想仅使用值而非HTML来设置条件,这是可能的吗?

感谢。

4 个答案:

答案 0 :(得分:0)

如果我理解,您可以使用ng-show:https://docs.angularjs.org/api/ng/directive/ngShow

<div ng-show="variable.value">{{variable.value}}</div>

答案 1 :(得分:0)

您可以执行以下操作。

"use babel"

angular.module('app', []).component('home', {
  controller: function() {
    this.$onInit = () => {
      this.name = 'David'
    }
  },
  template: `<form name="myForm">
  <label>I agree to terms
    <input type="checkbox" ng-model="$ctrl.terms">
  </label><br/>
  <p ng-if="$ctrl.terms">You have agreed to terms, {{$ctrl.name}}</p>
 </form>`
})

https://plnkr.co/edit/sXSVG56chFNNdPlfdsff?p=preview

答案 2 :(得分:0)

你最初初始化你的ng-if测试

NG-模型=&#34; myStaff&#34; ng-init =&#34; myStaff = true&#34;

之后

{{variable.value}}

答案 3 :(得分:0)

<div ng-if="option.isVisible">{{variable.value}}</div>

<div ng-show="option.isVisible">{{variable.value}}</div>