Angular.js和ng-switch-when-emulating枚举 [英] Angular.js and ng-switch-when - emulating enum
问题描述
我想在某些类型的安全性中为我的控制器逻辑引入一些枚举,所以例如我创建了一些这样的东西:
I wanted to introduce some enum to my controller logic for some type safety, so for example I created something like this:
var app = angular.module('myApp', []);
var StateEnum = Object.freeze({"login":1, "logout":2})
function LoginCheckCtrl($scope) {
$scope.stateEnum = StateEnum
$scope.loginData = StateEnum.login
$scope.login = function() {
console.log($scope.loginData ? 'logged in' : 'not logged in');
$scope.loginData = StateEnum.logout;
};
$scope.logout = function() {
console.log($scope.loginData ? 'logged in' : 'not logged in');
$scope.loginData = StateEnum.login;
};
}
在我的示例页面中,我会有这样的:
and in my example page I would have something like this:
<div ng-controller="LoginCheckCtrl">
<div ng-switch on="loginData">
<div ng-switch-when="stateEnum.login" ng-include="'login'"></div>
<div ng-switch-when="stateEnum.logout" ng-include="'logout'"></div>
</div>
</div>
<script type="text/ng-template" id="login">
<button ng-click="login()">Login</button>
</script>
<script type="text/ng-template" id="logout">
<button ng-click="logout()">Logout</button>
</script>
但 ng-switch-when
想工作只有当我用 ng-swith-when
手动替换整数时的值,例如1,2。
but ng-switch-when
does not want to work. It only works if I substitute values in ng-swith-when
manually with integers, for example 1,2.
以下是演示文稿的小费:
Here are fiddles to demonstrate this:
现在,你可以看到,第一个显然不起作用,第二个工作 - 意思是当按钮被点击时,它会改变按钮。
now, as you can see, the first one clearly does not work, and second one works - meaning it changes button when button is clicked.
我认为的问题是这个 var StateEnum = Object.freeze({登录:1,注销:2})
。
是否可以在我的html中使用我的枚举,所以 ng-switch-when
将正常工作(如第二个小提琴)?
Is is possible to use my enum in my html so ng-switch-when
will work properly (as in second fiddle)?
推荐答案
认为我会创建一个可以拥有所有枚举的服务:
I think I would create a service that could have all your enums:
angular.module('Enums', []).
factory('Enum', [ function () {
var service = {
freeze: {login:1, logout:2 },
somethingelse: {abc:1,def:2}
};
return service;
}]);
您的应用定义如下:
var app = angular.module('myApp', ['Enums']);
然后您的控制器可以在需要时注入它们:
Then your controllers you could inject them when you need them:
function LoginCheckCtrl($scope, Enum) {
if (1==Enum.freeze.login) // as an example
if (1==Enum.somethingelse.abc) // another example
服务是单身,所以这样会有效地你可以定义一组枚举。
Services are singletons so this effectively will give you a set of enums you could define.
对于指令中的ngSwitch,我相信它需要一个字符串(如果我错了,请更正我)。一对夫妇参考:
As for the ngSwitch when directive, I believe it requires a string (please correct me if I'm wrong). A couple references:
https:/ /groups.google.com/forum/?fromgroups#!topic/angular/EH4W0y93ZAA
https://github.com/angular/angular.js/blob/master/src/ng/directive/ngSwitch.js#L171
另一种实现想要的方法是使用 ng-show
/ ng-hide
An alternate way to achieve what you want would be to use ng-show
/ng-hide
<div ng-include="'login'" ng-show='stateEnum.login==loginData' ...>
这篇关于Angular.js和ng-switch-when-emulating枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!