如何在 angularjs 中使用 switch case 以及如何 return 值?

How to use switch case in angularjs and how can I return the value?

我创建了这样一个范围

var myApp = angular.module("myApp", []);
myApp.controller("MyController", function MyController($scope){ 
$scope.sample = [
{'NUMBER' : '1'},{'NUMBER' : '2'},{'NUMBER' : '3'},{'NUMBER' : '4'}
]
});

我试过像 this.In 这样我需要从 switch case 中检索 return 的值并将其显示在特定字段中。

<div  ng-repeat = "grp_val in sample">
<div ng-switch-on="{{grp_val.NUMBER}}">
   <div ng-switch-when="1" ng-init="value='ONE'"></div>
   <div ng-switch-when="2" ng-init="value='TWO'"></div>
   <div ng-switch-when="3" ng-init="value='THREE'"></div>
   <div ng-switch-when="4" ng-init="value='FOUR'"></div>
</div>
 <label>{{VALUE}}</label>
</div>

但它没有working.Kindly给出一些解决方案。

您已将 ng-repeat 用于 div,并试图在 ng-repeat 中使用 ng-switch,这不是您尝试实施的正确解决方案。

ng-switch-on 的语法不正确。应该是 ng-switchon=Expression。检查 Documentation

您尝试使用 ng-init 初始化值的方式也不起作用,因为当代码首次执行时,所有具有 ng-switch-when 的 div 都不会初始化值,而当 selected div 变得可见,它不是来自 div 的初始值,因为生命周期。

检查工作 demo 和工作 ng-switch