简体   繁体   English

如何从前端处理后端枚举

[英]How to handle Enums from backend in frontend

I use at frontend AngularJS 1.4 and at Backend Java and I have a lot of options which can be selected in frontend, eg the country: 我在前端AngularJS 1.4和后端Java上使用,我有很多可以在前端选择的选项,例如国家/地区:

GERMANY
FRANCE
USA
RUSSIA

Enums are written in upper case and I will customize this in frontend (eg FRANCE will become to France). 枚举用大写字母书写,我将在前端对其进行自定义(例如,法国将成为法国)。

My question now would be if there is a directive or a any other support doing this in frontend. 我的问题现在是在前端是否有指令或其他任何支持。

Check out this link http://hello-angularjs.appspot.com/angularjs-create-custom-filter 查看此链接http://hello-angularjs.appspot.com/angularjs-create-custom-filter

It has a custom filter written which can help you out. 它具有编写的自定义过滤器,可以为您提供帮助。

Here's code from link given above. 这是上面给出的链接中的代码。

<!DOCTYPE html>

<html ng-app="HelloApp">
<head>
    <title></title>
</head>
<body ng-controller="HelloCtrl">
  <form>
      <input type="text" ng-model="name"/>
    </form>
    <div>{\{name|titlecase}}</div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script type="text/javascript">
        // Code defining custom module consisting of a filter
        // The module needs to be included as dependency for using the filter, titlecase
        //
        angular.module('CustomFilterModule', [])
            .filter( 'titlecase', function() {
                return function( input ) {
                    return input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
                }
            });
        // Angular App on this page 
        // Included CustomFilterModule as dependency
        //
        angular.module('HelloApp', [ 'CustomFilterModule'])
            .controller('HelloCtrl', ['$scope', function($scope){
                $scope.name = '';
            }])
    </script>
</body>
</html>

In view: 鉴于:

<select ng-model="selectedCountry" ng-options="country as country.name for country in countries | filter:filterUpperCamelCase"></select>

In controller: 在控制器中:

   $scope.countries= [
     {name:"SPAIN"}, 
     {name:"GERMANY"}
   ];

    $scope.filterUpperCamelCase = function(element){
        element.name = element.name.toLowerCase();
        element.name = element.name.charAt(0).toUpperCase() + element.name.slice(1);
        return element;
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM