Angular JS有条件地应用过滤器

时间:2016-05-11 19:13:23

标签: angularjs

平均产品和渠道定价:{{average_price |货币}}

average_price是一个字符串,其初始值为“None”。它不能是0.0。

当我应用货币过滤器时,最初不显示值“无”。

我希望只有当average_price采用货币/数字格式时才应用货币过滤器。

1 个答案:

答案 0 :(得分:1)

添加条件怎么样。

<head>
  <base href="https://polygit.org/polymer+:master/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="paper-card/paper-card.html">
</head>

<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <template is="dom-repeat" items="{{books}}" as="book">
        <paper-card>
          <div><b>Title: </b><span>{{book.title}}</span>
          </div>
          <div><b>Author: </b><span>{{book.author}}</span>
          </div>
          <div>
            <p>Votes:</p>
            <div>
              <b>Bad: </b><span>{{book.votes.bad}}</span>
              <b>Regular: </b><span>{{book.votes.regular}}</span>
              <b>Good: </b><span>{{book.votes.good}}</span>
              <b>Excellent: </b><span>{{book.votes.excellent}}</span>
            </div>
          </div>
        </paper-card>
      </template>
    </template>
    <script>
      Polymer({
        is: 'x-foo',
        properties: {
          books: {
            type: Array,
            value: function() {
              return [{
                "title": "My title book",
                "author": "The author",
                "votes": {
                  "bad": 0,
                  "regular": 2,
                  "good": 201,
                  "excellent": 458
                }
              }, {
                "title": "The other book",
                "author": "The other author",
                "votes": {
                  "bad": 11,
                  "regular": 22,
                  "good": 33,
                  "excellent": 44
                }
              }];
            }
          }
        }
      });
    </script>
  </dom-module>
</body>

或者,如果{{(average_price | currency) || average_price}} 过滤器

的值无效,您可以提供默认值
currency

Demo