Angular 将特定数据检索到 $scope 变量中不起作用 [英] Angular retrieve specific data into $scope variable is not working
问题描述
我在这里初始化
$scope.statuses = [];
然后,如果我只是将 $http.get 中的数据设置为 $scope 变量,那有效",但我需要对其进行更多过滤.
$scope.statuses = result.data.Devices;控制台日志($scope.statuses);
在开发工具控制台输出中返回这样的数据数组
<块引用>0:对象$$哈希键:对象:5"援助:oAAABTUAAg=="指数:DKi00000002"默认有效载荷:"C:\ProgramData\"设备编号:00022B9A000000010001"设备状态:3清单 ID 列表:数组[3]待处理清单 ID:空值待定时间戳:0001-01-01T00:00:00"沙:"R2tiZRQgY/iohXZt5O4HaQwtVe/adWU2VOcKaelJ3Us="StagedManifestIdList:数组[0]
但是我只想要一些特定的数据
$scope.statuses = result.data.Devices.DeviceStatus;
为什么显示未定义",我该怎么做?
所以 0:对象 DeviceStatus 在那里.. :/
...<tr ng-repeat="vm.devices 中的设备"><td>{{device.DeviceId}}</td><td>{{device.DeviceStatus}}</td><td>{{device.Aid}}</td><td>{{device.Sha}}</td></tr>...本质上,我希望在 Javascript/angular (.js) 中的数据进入 ng-repeat 循环之前对其进行操作
因此 $scope 甚至是正确使用的东西吗?
我知道我有一些数据要更改,例如
字段中的某些数据被 [] 包围,例如[01,02,03] 这样做{{ device.aid.join(',') }} 会修复" [] 问题,但我需要一些这样的功能?我在哪里可以使用它?
//用法 {{displayArchived(item.archives)}}$scope.displayArchived = function(archives){返回 (archives.length > 0) ?archives.join(',') : '无';};
那么让"有助于获取 DeviceStatus 的数量吗?
let statuses = ['旧设备', '新设备', '已激活', '未激活'];
解决方案 在这个例子中,result.data.Devices
是一个看起来像这样的对象数组:
<预><代码>[{DeviceId : "00022B9A000000010001" DeviceStatus : 3 .... },{DeviceId : "00022B9A000030011111" DeviceStatus : 9 ...},....]因此,当您尝试获取 result.data.Devices.DeviceStatus
时,没有名为 DeviceStatus
的数组元素,这就是您返回 undefined 的原因代码>.
您需要遍历设备数组以获取特定设备的DeviceStatus
:
angular.forEach(result.data.Devices, function(value, index){$scope.statuses.push(value.DeviceStatus);});
或者您可以直接访问,如果您知道您想要的设备:
var index = 1;$scope.statuses = result.data.Devices[index].DeviceStatus;
如果您想获取所有设备并使用模板显示 {{ device.DeviceStatus }},这里有一个解决方案:
代码:
$scope.devices = result.data.Devices;
模板:
{{ device.DeviceStatus }}
在我们的代码中,我们将 request.data.Devices
数组分配给 $scope.devices
.然后在模板中,我们使用 ng-repeat
遍历 $scope.devices
中的每个 device
并显示设备的 DeviceStatus代码>.
为了将 DeviceStatus 与其实际名称相匹配,您可以创建自定义过滤器.
在您的代码中创建过滤器:
app.filter('deviceStatus', function () {返回函数(status_id){var statuses = ['旧设备', '新设备', '已激活', '未激活'];返回状态[status_id];};});
您现在可以在模板中使用新过滤器:
{{device.DeviceId |设备状态}}</td>我们将 DeviceId
传送到我们的自定义 deviceStatus
过滤器中,以便在模板中获得正确的状态名称.
I initialize here
$scope.statuses = [];
Then if I simply set the data from $http.get to the $scope variable, that "works" but I need to filter it down more.
$scope.statuses = result.data.Devices;
console.log($scope.statuses);
That returns the array of data like this in dev tools console output
0: Object
$$hashKey
:
"object:5"
Aid
:
"oAAABTUAAg=="
DKiIndex
:
"DKi00000002"
DefaultPayload
:
"C:\ProgramData\"
DeviceId
:
"00022B9A000000010001"
DeviceStatus
:
3
ManifestIdList
:
Array[3]
PendingManifestId
:
null
PendingTimeStamp
:
"0001-01-01T00:00:00"
Sha
:
"R2tiZRQgY/iohXZt5O4HaQwtVe/adWU2VOcKaelJ3Us="
StagedManifestIdList
:
Array[0]
However I only WANT some specific data
$scope.statuses = result.data.Devices.DeviceStatus;
Why does it say 'undefined' and how do I do this?
So 0: Object DeviceStatus is there.. :/
<div ng-app="app" ng-controller="DeviceController as vm">
...
<tr ng-repeat="device in vm.devices">
<td>{{device.DeviceId}}</td>
<td>{{device.DeviceStatus}}</td>
<td>{{device.Aid}}</td>
<td>{{device.Sha}}</td>
</tr>
...
Essentially I wish to manipulate the data in Javascript /angular (.js) before it makes it to the ng-repeat loop
Thus is $scope even the proper thing to even be using?
I know that I have some data to change for example
Some data in a field was surrounded by [] e.g. [01,02,03] so doing
{{ device.aid.join(',') }} would "fix" the [] issue but i need to have some function like this? where can i use this?
// usage {{displayArchived(item.archives)}}
$scope.displayArchived = function(archives){
return (archives.length > 0) ? archives.join(',') : 'none';
};
Then will a "let" help for numbers of DeviceStatus?
let statuses = ['Old Device', 'New Device', 'Activated', 'Unactivated'];
解决方案 In this instance, result.data.Devices
is an array of objects that looks something like this:
[
{DeviceId : "00022B9A000000010001" DeviceStatus : 3 .... },
{DeviceId : "00022B9A000030011111" DeviceStatus : 9 ...},
....
]
So when you try to get result.data.Devices.DeviceStatus
, there is no array element called DeviceStatus
, which is why you are getting back undefined
.
You will need to iterate through the array of Devices to get a specific Device's DeviceStatus
:
angular.forEach(result.data.Devices, function(value, index){
$scope.statuses.push(value.DeviceStatus);
});
or you can access directly if you know which device you want:
var index = 1;
$scope.statuses = result.data.Devices[index].DeviceStatus;
EDIT:
If you want to get all of the devices and display {{ device.DeviceStatus }} with your template, here is a solution:
Code:
$scope.devices = result.data.Devices;
Template:
<div ng-repeat="device in devices">
{{ device.DeviceStatus }}
</div>
In our code, we assign the request.data.Devices
array to $scope.devices
. Then in the template, we use ng-repeat
to go through each device
in $scope.devices
and display the device's DeviceStatus
.
EDIT2:
In order to match the DeviceStatus to it's actual name, you can create a custom filter.
In your code create the filter:
app.filter('deviceStatus', function () {
return function (status_id) {
var statuses = ['Old Device', 'New Device', 'Activated', 'Unactivated'];
return statuses[status_id];
};
});
You can use the new filter in your template now:
<td>{{device.DeviceId | deviceStatus}}</td>
We pipe the DeviceId
into our custom deviceStatus
filter in order to get the correct status name in the template.
这篇关于Angular 将特定数据检索到 $scope 变量中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆