void DA_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
if (e.Status == UpdateStatus.ErrorsOccurred)
{
e.Row.RowError = e.Errors.Message;
e.Status = UpdateStatus.SkipCurrentRow;
throw new Exception(e.Errors.Message);
}
<snip>
}
void DA_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
try
{
if (e.Status == UpdateStatus.ErrorsOccurred)
{
var ex = new Exception(e.Errors.Message);
throw ex;
}
<snip>
包含itemsService
函数,该函数检查答应成功是否将项目添加到items数组并将其传递给additem
控制器
没关系
现在,我还想存储承诺结果并将其传递给shoppinglist
中的控制器
不能在dom中与list.status
一起使用此属性,以在承诺结果为拒绝时显示错误
ng-if
(function() {
'use strict';
angular
.module('shopping', [])
.controller('shoppinglist', shoppinglist)
.service('itemsService', itemsService)
.service('weightControllerService', weightControllerService);
shoppinglist.$inject = ['itemsService'];
function shoppinglist(itemsService) {
let list = this;
list.Quantity = '';
list.Name = '';
list.additem = () => {
itemsService.additem(list.Quantity, list.Name);
//
list.status = itemsService.status;
};
list.removeitem = $index => {
itemsService.removeitem($index);
};
list.items = itemsService.getitems();
}
itemsService.$inject=['weightControllerService']
function itemsService(weightControllerService) {
let service = this;
let items = [];
service.additem = (N, Q) => {
let namePromise = weightControllerService.nameCheck(N);
namePromise
.then(resolve => {
let item = { Name: N, Quantity: Q };
items.push(item);
// how can i return the resolve to show or hide error at dom
service.status = resolve;
})
.catch(reject => {
// how can i return the resolve to show or hide error at dom
service.status = reject;
});
};
service.removeitem = $index => {
items.splice($index, 1);
};
service.getitems = () => {
return items;
};
}
答案 0 :(得分:0)
对于有诺言的人,将return
语句用于已解决的诺言,并将throw
语句用于已拒绝的诺言:
itemsService.additem = (N, Q) => {
let namePromise = weightControllerService.nameCheck(N);
var newPromise = namePromise
.then(resolve => {
let item = { Name: N, Quantity: Q };
items.push(item);
// how can i return the resolve to show or hide error at dom
service.status = resolve;
//RETURN value
return items;
})
.catch(reject => {
// how can i return the resolve to show or hide error at dom
service.status = reject;
//THROW reason
throw reject;
});
//RETURN new promise
return newPromise;
};
然后在控制器中使用返回的promise:
list.additem = () => {
let promise = itemsService.additem(list.Quantity, list.Name);
promise.then(items => {
console.log(items);
})
.catch(reject => {
console.log("additem failed");
console.log(reject);
});
//
//list.status = itemsService.status;
};
此.then
和.catch
方法返回一个新的诺言,该诺言通过successCallback
,errorCallback
的返回值(或抛出)来解决或拒绝(除非该值是一个承诺,在这种情况下,它将使用promise chaining来解决该承诺中所解析的值。
有关更多信息,请参见