如何在 Angular JS 中执行开关或 else/if 语句
how to do a switch or else/if statement in angular js?
我是angular js的新手,并且正在尝试使用它,以使我的代码在用户输入时返回国家/地区的国旗。到目前为止,当他们开始输入一个国家/地区时,我的列表会过滤掉,但它不会显示国旗。法典:
<h2> Section Two </h2>
<div ng-app="" ng-controller="ModuleTwoController">
<p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in filtered = (countries | filter:test)">
{{ x.country }}
</li>
</ul>
<p> Items in list: {{filtered.length}} </p>
<div ng-switch on="countries">
<div ng-switch-when="filtered.value === "Argentina">
<img src="argentina.jpg" height="150" width="200">
</div>
<div ng-switch-when="USA">
<img src="usa.jpg" height="150" width="200">
</div>
<div ng-switch-when="Brazil">
<img src="brazil.jpg" height="150" width="200">
</div>
<div ng-switch-when="Hong Kong">
<img src="hongkong.jpg" height="150" width="200">
</div>
<div ng-switch-when="UK">
<img src="uk.jpg" height="150" width="200">
</div>
<div ng-switch-when="Turkey">
<img src="turkey.jpg" height="150" width="200">
</div>
<div ng-switch-when="Rwanda">
<img src="rwanda.jpg" height="150" width="200">
</div>
<div ng-switch-when="Federated States of Micronesia">
<img src="fsom.jpg" height="150" width="200">
</div>
<div ng-switch-when="India">
<img src="india.jpg" height="150" width="200">
</div>
<div ng-switch-when="South Africa">
<img src="southafrica.jpg" height="150" width="200">
</div>
我需要添加什么才能使它为我提供我想要的结果?我也只希望当列表中有一个国家时出现国旗。因此,例如,如果我输入"U",则美国,英国,土耳其和南非都将返回。但是,如果我输入"我们",则只返回美国。这是我想要返回标志的唯一一点。
这是您的解决方案代码 请检查
.HTML
<h2> Section Two </h2>
<div ng-app="myApp" ng-controller="ModuleTwoController">
<p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in filtered = (countries | filter:test)">
{{ x.country }}
</li>
</ul>
<p> Items in list: {{filtered.length}} </p>
<div ng-show="filtered.length == 1" >
<div ng-switch on="filtered[0].country">
<div ng-switch-when="Argentina">
<img src="Argentina.jpg" height="150" width="200">
</div>
<div ng-switch-when="USA">
<img src="usa.jpg" height="150" width="200">
</div>
<div ng-switch-when="Brazil">
<img src="brazil.jpg" height="150" width="200">
</div>
<div ng-switch-when="Hong Kong">
<img src="hongkong.jpg" height="150" width="200">
</div>
<div ng-switch-when="UK">
<img src="uk.jpg" height="150" width="200">
</div>
<div ng-switch-when="Turkey">
<img src="turkey.jpg" height="150" width="200">
</div>
<div ng-switch-when="Rwanda">
<img src="rwanda.jpg" height="150" width="200">
</div>
<div ng-switch-when="Federated States of Micronesia">
<img src="fsom.jpg" height="150" width="200">
</div>
<div ng-switch-when="India">
<img src="india.jpg" height="150" width="200">
</div>
<div ng-switch-when="South Africa">
<img src="southafrica.jpg" height="150" width="200">
</div>
<div ng-switch-default>
Nothing
</div>
</div>
</div>
</div>
Js代码
var app = angular.module('myApp', []);
function ModuleTwoController($scope) {
$scope.countries = [{country:'Argentina'},
{country:'USA'},
{country:'Brazil'},
{country:'Hong Kong'},
{country:'UK'},
{country:'Turkey'},
{country:'Rwanda'},
{country:'Federated States of Micronesia'},
{country:'India'},
{country:'South Africa'}
];
}
这是工作示例小提琴
相关文章:
- Jquery else if statement
- Javascript skips else if?
- JqueryUI,多个ID和else if 语句在可丢弃,我如何让它们工作
- JQuery Else if statement?
- 以这种方式使用if/else-if/else是一种很好的做法
- switch(true)作为else-if的替代
- else-if子句未按预期工作
- 使用if-else-if-true检查(验证)单选按钮如果未检查则无警报必须检查警报
- Javascript else-if语句错误
- `else if `从不执行;只执行前面的“if”
- 正在创建嵌套的if+else-if语句,但函数返回为未定义
- else-if条件在js渲染模板中
- 我的if和else-if逻辑中的问题以及两个函数的冲突
- 带有if语句的循环的Javascript未到达else-if语句
- 使用 If else if else 语句得到错误
- if-else-if JavaScript 返回 true 并退出,无论输入如何
- Jquery else if is not working
- 如何在 Angular JS 中执行开关或 else/if 语句
- 我可以在 html 文档中使用 if/else if 命令吗?
- 我需要帮助“调试”If/Else if/Else 语句的语法