将$scope的一部分角度编码为json
我正在使用angular,并且我的大部分api都在运行,但是我在使用put函数时遇到了问题。我的服务器由arduino托管,无法接收json stringify发送的所有数据 如何使用特定的$scope数据生成JSON obj 这就是我到目前为止所知道的,有“chanObj”。在某些测试中,警报返回“未定义”,在其他测试中,警报在作用域末尾返回空对象将$scope的一部分角度编码为json,json,angularjs,Json,Angularjs,我正在使用angular,并且我的大部分api都在运行,但是我在使用put函数时遇到了问题。我的服务器由arduino托管,无法接收json stringify发送的所有数据 如何使用特定的$scope数据生成JSON obj 这就是我到目前为止所知道的,有“chanObj”。在某些测试中,警报返回“未定义”,在其他测试中,警报在作用域末尾返回空对象 $scope.setpointClk = function(channel, setpoint) { var channels = J
$scope.setpointClk = function(channel, setpoint) {
var channels = JSON.stringify($scope.channels); //original obj to big
var chanObj = {setPoint : channel.setPoint, name : channel.name
}; //try to create a selected content obj
alert(chanObj);
service.putChannels(channels, function() {
$scope.init();
});
}
channels: [{name: null, canal: "0", status: false, temperature: 19.94, setPoint: 0, permission: false,…},…]
0: {name: null, canal: "0", status: false, temperature: 19.94, setPoint: 0, permission: false,…}
1: {name: null, canal: "1", status: false, temperature: 20.37, setPoint: 5, permission: false,…}
2: {name: null, canal: "2", status: false, temperature: 0, setPoint: 5, permission: false, percentOut: 0}
3: {name: null, canal: "3", status: false, temperature: 0, setPoint: 5, permission: false, percentOut: 0}
4: {name: null, canal: "4", status: false, temperature: 19.94, setPoint: 5, permission: false,…}
5: {name: null, canal: "5", status: false, temperature: 19.87, setPoint: 5, permission: false,…}
6: {name: null, canal: "6", status: false, temperature: 19.98, setPoint: 5, permission: false,…}
7: {name: null, canal: "7", status: false, temperature: 19.96, setPoint: 5, permission: false,…}
8: {name: null, canal: "8", status: false, temperature: -50, setPoint: 5, permission: false,…}
9: {name: null, canal: "9", status: false, temperature: -50, setPoint: 5, permission: false,…}
只需创建一个新对象并将其传递给
put
调用,就像使用chanObj
对象一样,将其传递给put
如果要维护很多属性,可以执行以下操作:
var propertiesToCopy = ["setPoint", "name", "foo", "bar"],
cutdownVersion = {};
for(var key in propertiesToCopy) {
cutdownVersion[key] = channels[key];
}
然后将
cutdownVersion
对象传递给put调用。只需创建一个新对象并将其传递给put
调用,就像您使用chanObj
对象一样,将其传递给put调用
如果要维护很多属性,可以执行以下操作:
var propertiesToCopy = ["setPoint", "name", "foo", "bar"],
cutdownVersion = {};
for(var key in propertiesToCopy) {
cutdownVersion[key] = channels[key];
}
然后将
cutdownVersion
对象传递给put调用。您应该在不调用JSON.stringify
函数的情况下创建chanObj
。因为这会将$scope.channels
转换为字符串
例如,关于如何从$scope.channels
$scope.setpointClk = function(channel, setpoint) {
...
var chanObj = {
setPoint: $scope.channels.setPoint,
name: $scope.channels.name
};
...
}
您应该创建
chanObj
,而不必首先调用JSON.stringify
函数。因为这会将$scope.channels
转换为字符串
例如,关于如何从$scope.channels
$scope.setpointClk = function(channel, setpoint) {
...
var chanObj = {
setPoint: $scope.channels.setPoint,
name: $scope.channels.name
};
...
}
@Nitrof,它是一个对象,不要
警告它,使用控制台.log(chanObj)
,添加从浏览器开发工具控制台日志查看它仍然返回:对象{设定点:未定义,名称:未定义}找到它!我必须将$scope注入函数$scope.setpointClk=function($scope,channel){var chanObj={setPoint:$scope.setPoint,name:$scope.name};@Nitrof,它是一个对象,不要警告它,使用console.log(chanObj)
,添加并从浏览器开发工具控制台logstill返回:object{setPoint:undefined,name:undefined}找到了!我必须将$scope注入函数$scope.setpointClk=function($scope,channel){var chanObj={setPoint:$scope.setPoint,name:$scope.name};