有人可以告诉我比较ngModel旧值和新值的最佳做法是什么?
角度1:
$scope.$watch('someProperty', funciton(oldVal, newVal){
// code goes here
})
Run Code Online (Sandbox Code Playgroud)
我问这个,因为(ngModelChange)从来没有带给我oldVal,只有newVal.
在我的例子中,我在<select>标签中使用ngModel 并将旧选择与新选择进行比较:
<select [(ngModel)]="current" (ngModelChange)="onModelChange($event)">
<option *ngFor="let item of myArray" [ngValue]="item">{{item.name}} </option>
</select>
Run Code Online (Sandbox Code Playgroud) 我试图通过@Output传递参数,但被激活的函数只是接收'undefined'.有人可以告诉我通过@Output的EventEmitter传递参数的方法吗?例如:
var childCmp = ng.core.Component({
selector:'child-cmp',
outputs: ['myEvent']
}).Class({
constructor: function(){
this.myEvent = new ng.core.EventEmitter();
this.myEvent.emit(false);
}
});
var parentCmp = ng.core.Component({
selector:'parent-cmp',
template:'<child-cmp (myEvent)="invoke()"'></child-cmp>',
directives: [childCmp]
}).Class({
constructor:function(){},
invoke: function(flag){
// here flag is undefined!!
}
});
Run Code Online (Sandbox Code Playgroud) 有没有办法通过服务名称获得角度为2的可注射服务的实例?
例如,在Angular 1中你可以写:
var service = $injector.get('ServiceName');
Run Code Online (Sandbox Code Playgroud)
并且service变量将获得服务的实例.
我真的很感谢你的帮助!