所以我在angularjs服务器中有一个方法,它调用一个方法,为数组中的每个方法返回一个promise.我使用下划线_each循环遍历数组.我想等到处理整个数组之后再调用方法中的最后一行代码.
所以...
function ProcessCoolStuff(coolStuffs)
{
var stuff = [];
_.each(coolStuffs, function(coolStuff)
{
//Some method using $q to return
makeStuffCooler(coolStuff).then(function(coolerStuff)
{
stuff.push(coolerStuff);
});
});
//Maybe Call a Display Method, or call event ect..
ShowAllMyCoolStuff(stuff);
}
Run Code Online (Sandbox Code Playgroud)
这当然不起作用..循环完成并在为每个项目完成makeStuffCooler之前调用'ShowAllMyCoolStuff'.那么..与异步方法交互的正确方法是什么,所以我的ShowAllMyCoolStuff方法将等到集合被填充?这可能是我缺乏$ q和承诺的经验,但我被困住了.提前致谢.
我使用Angular 2 ngModel绑定遇到了一个问题. plnkr
如果我使用ngModel将值绑定到子组件,则不会在子组件的OnInit函数上填充该值.所以,如果我绑定一个属性调用"boundName",我尝试在OnInit中访问它,它将为null.但是,如果我将父控件中的相同值绑定到不使用ngModel而是输入参数,则可以在OnInit函数中访问该值.
所以..如果我的父组件创建一个子组件,如
<my-boundcomp [(ngModel)]="name" [(inputName)] ="name" ></my-boundcomp>
Run Code Online (Sandbox Code Playgroud)
我的子组件onInit函数是
public ngOnInit() {
console.log("Input Name :" + this.inputName);
console.log("Bound Name :" + this.boundName);
this._boundNameOnInit = this.boundName; // <--- Going to be null
this._inputNameOnInit = this.inputName; // <--- Going to be not null
Run Code Online (Sandbox Code Playgroud)
}
我发现这种行为很奇怪,而且出乎意料.我不确定这是一个错误还是我没有正确使用FormsModule ngModel,但有趣的是我认为我应该分享.
这是完整的plnkr https://plnkr.co/edit/Im5oz7q1HhG5MgGTTZ1R?p=preview