具有多个条件的 AngularJS ng-if [英] AngularJS ng-if with multiple conditions

查看:19
本文介绍了具有多个条件的 AngularJS ng-if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能有这样的东西:

div ng-repeat="(k,v) in items"<div ng-if="k == 'a' || k == 'b'"><!-- 一些内容-->

知道 items 是通过请求接收的 JSON 容器,所以这就是我使用键值方法的原因.

谢谢

我这么问是因为我试过用谷歌搜索它,但我能得到的唯一结果是使用 ng-switch,但我必须使用 ng-if.

解决方案

当然可以.类似的东西:

HTML

<label ng-repeat="(key,val) in list"><input type="radio" name="localityTypeRadio" ng-model="$parent.localityTypeRadio" ng-value="key"/>{{key}}<div ng-if="key == 'City' || key == 'County'"><预>市或县!!!{{$parent.localityTypeRadio}}</pre>

<div ng-if="key == '镇'"><预>镇!!!{{$parent.localityTypeRadio}}</pre>

JS

var fessmodule = angular.module('myModule', []);fessmodule.controller('fessCntrl', function ($scope) {$scope.list = {城市:[{name: "cityA"}, {name: "cityB"}],县:[{名称:countyA"},{名称:countyB"}],城镇:[{名称:townA"},{名称:townB"}]};$scope.localityTypeRadio = '城市';});fessmodule.$inject = ['$scope'];

演示 小提琴

I'd like to know if it's possible to have something like this:

div ng-repeat="(k,v) in items"
<div ng-if="k == 'a' || k == 'b'">
    <!-- SOME CONTENT -->
</div>

Knowing that items is a JSON container received through a request, so that's why I'm using a key, value method.

Thanks

I'm asking because I've tried googling it, but the only result I could get were with ng-switch, but I have to use ng-if.

解决方案

Sure you can. Something like:

HTML

<div ng-controller="fessCntrl">    
     <label ng-repeat="(key,val) in list">
       <input type="radio" name="localityTypeRadio" ng-model="$parent.localityTypeRadio" ng-value="key" />{{key}}
         <div ng-if="key == 'City' || key == 'County'">
             <pre>City or County !!! {{$parent.localityTypeRadio}}</pre>
         </div>
         <div ng-if="key == 'Town'">
             <pre>Town!!! {{$parent.localityTypeRadio}}</pre>
         </div>        
      </label>
</div>

JS

var fessmodule = angular.module('myModule', []);

fessmodule.controller('fessCntrl', function ($scope) {

    $scope.list = {
        City: [{name: "cityA"}, {name: "cityB"}],
        County: [{ name: "countyA"}, {name: "countyB"}],
        Town: [{ name: "townA"}, {name: "townB"}]
      };

    $scope.localityTypeRadio = 'City';
});

fessmodule.$inject = ['$scope'];

Demo Fiddle

这篇关于具有多个条件的 AngularJS ng-if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆