我有一个返回一些数据的Web服务。为了防止代码重复,我想将http请求调用移动到角度服务:
angular.module('abc', [])
.factory('def', function () {
return {
getData: function () {
return $http.post('/path/', {});
}
};
});
一切都很好,但必要的数据在复杂的对象中,我每次都要写:
def.getData().then(function (response) {
scope.obj = response.data.qwe.rty.xyz;
});
返回承诺的最简单方法是什么,它会将response.data.qwe.rty.xyz
的值直接发送到successCallback
?我可以这样写:
def.getData().then(function (obj) {
scope.obj = obj;
});
答案 0 :(得分:4)
致电$http.post('/path/', {})
会返回一个承诺,然后您可以致电then()
。请注意,then()
也会返回一个承诺,因此您可以对调用进行链接。因此,您的代码可能如下所示:
angular.module('abc', [])
.factory('def', function () {
return {
getData: function () {
return $http.post('/path/', {})
.then(function(response) {
return response.data.qwe.rty.xyz;
});
}
};
});
答案 1 :(得分:2)
您可以使用Private Sub UserForm_Initialize()
Dim RowMax As Integer
Dim wsh As Worksheet
Dim countExit As Integer
Dim CellCombo1 As String
Dim i As Integer
Dim j As Integer
Set wsh = ThisWorkbook.Sheets("Sheet2")
RowMax = wsh.Cells(Rows.Count, "A").End(xlUp).Row
'find last row of sheet in column A
ComboBox1.Clear
'clear all value of comboBox1
With ComboBox1
For i = 2 To RowMax
'Run each row of column A
countExit = 0
CellCombo1 = wsh.Cells(i, "A").Value
For j = i To 2 Step -1
'just show value not duplicate
If CellCombo1 = wsh.Cells(j, "A").Value Then
countExit = countExit + 1
End If
Next j
If countExit = 0 Then
ElseIf countExit > 1 Then
Else
.AddItem CellCombo1
End If
Next i
End With
End Sub
Private Sub ComboBox1_Change()
Dim RowMax As Integer
Dim wsh As Worksheet
Dim countExit As Integer
Dim CellCombo2 As String
Dim i As Integer
Set wsh = ThisWorkbook.Sheets("Sheet2")
RowMax = wsh.Cells(Rows.Count, "A").End(xlUp).Row
'find last row of sheet in column A
ComboBox2.Clear
'clear all value of comboBox2
With ComboBox2
For i = 2 To RowMax
If wsh.Cells(i, "A").Value = ComboBox1.Text Then
'Just show value of mapping with column A
.AddItem wsh.Cells(i, "B").Value
Else
End If
Next i
End With
End Sub
提供程序
$q
并在您的控制器中使用它,如:
angular.module('abc', [])
.factory('def', function ($q) {
return {
getData: function () {
var def = $q.defer
$http.post('/path/', {}).then(function(response){
def.resolve(response.data.qwe.rty.xyz)
});
return def.promise;
}
};
});