如何通过属性引用一个对象的值?
How to reference a value of an object by property?
我有一个这样的 angularjs 对象:
$scope.afterLogin = [
{value: 'customers|follow-ups', text: 'Follow Ups'},
{value: '', text: 'not set'}
];
我正在尝试将其与 xeditable 一起使用,如下所示:
<span
editable-select="user.default_module"
e-ng-options="s.value as s.text for s in afterLogin"
e-name="default_module"
e-form="rowform">{{s[user.default_module] as s.text for s in afterLogin}}</span>
我正在尝试的是在 user.default_module 定义的 afterLogin 中显示文本 -属性。我究竟做错了什么?我在 s[user.default_module] as 上收到解析错误 - 如何在此范围内引用对象的 属性?
注意:这是用ng-repeat="user in users"
包裹的。
您试图以一种非常奇怪的方式显示当前选择的值。在 official example 中,他们为此目的使用过滤器。但是,您可以通过构建 value: label
地图来简化它,即:
$scope.afterLoginLabels = {};
for (var i = 0; i < $scope.afterLogin.length; i++) {
$scope.afterLoginLabels[$scope.afterLogin[i].value] = $scope.afterLogin[i].text;
}
然后,将文本值显示为:
<span [...]>{{ afterLoginLabels[user.default_module] }}</span>
见JSFiddle。
我有一个这样的 angularjs 对象:
$scope.afterLogin = [
{value: 'customers|follow-ups', text: 'Follow Ups'},
{value: '', text: 'not set'}
];
我正在尝试将其与 xeditable 一起使用,如下所示:
<span
editable-select="user.default_module"
e-ng-options="s.value as s.text for s in afterLogin"
e-name="default_module"
e-form="rowform">{{s[user.default_module] as s.text for s in afterLogin}}</span>
我正在尝试的是在 user.default_module 定义的 afterLogin 中显示文本 -属性。我究竟做错了什么?我在 s[user.default_module] as 上收到解析错误 - 如何在此范围内引用对象的 属性?
注意:这是用ng-repeat="user in users"
包裹的。
您试图以一种非常奇怪的方式显示当前选择的值。在 official example 中,他们为此目的使用过滤器。但是,您可以通过构建 value: label
地图来简化它,即:
$scope.afterLoginLabels = {};
for (var i = 0; i < $scope.afterLogin.length; i++) {
$scope.afterLoginLabels[$scope.afterLogin[i].value] = $scope.afterLogin[i].text;
}
然后,将文本值显示为:
<span [...]>{{ afterLoginLabels[user.default_module] }}</span>
见JSFiddle。