[英]Upgrade AngularJS directive with ngModel to Angular component
I am trying to upgrade my AngularJS directive to Angular Component.我正在尝试将我的 AngularJS 指令升级到 Angular 组件。
Here is the code of directive:这是指令的代码:
ng1AppModule.component('ng1Tmp', {
bindings: {p: '<'},
require: {ngModel: 'ngModel'}
});
And I tried to upgrade it by:我尝试通过以下方式升级它:
@Directive({selector: 'ng1-tmp'})
class Ng1HTmpComponent extends UpgradeComponent{
@Input() p: string;
@Input() ngModel: INgModelController;
constrcutor(elementRef: ElementRef, injector: Injector) {
super('ng1Tmp', elementRef, injector);
}
}
It doesn't work well.它不能很好地工作。 It seems not to support ngModel to upgrade in this way.
好像不支持ngModel这种方式升级。 But I don't see any related information in this documentation: https://angular.io/guide/upgrade .
但我在本文档中没有看到任何相关信息: https : //angular.io/guide/upgrade 。
Does anyone have some ideas on this?有没有人对此有一些想法?
Thanks in advance.提前致谢。 :)
:)
Angular not mean new version of AngularJS, them are different struct. Angular 并不是 AngularJS 的新版本,它们是不同的结构。 3 point must known before code one component: Input, Output and EventEmitter.
编写代码前必须知道的 3 点组件:Input、Output 和 EventEmitter。
One example Directive/Component here may can some help.这里的一个示例指令/组件可能会有所帮助。
//AngularJS directive, use it like <pagination></pagination>
app.register.directive('pagination', function () {
return {
restrict : 'E',
scope : true,
templateUrl : '/views/widgets/pagination.html',
controllerAs : 'vm',
controller : PaginationController
};
PaginationController.$inject = ['$scope'];
function PaginationController($scope) {
}
});
And now I was redefined it on Angular.现在我在 Angular 上重新定义了它。
//Angular6 Component, use it like
//<app-pagination [pager]="pagination" (fired)="onFire($event)"></app-pagination>
import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-pagination',
templateUrl: './pagination.component.html',
styleUrls: ['./pagination.component.scss']
})
export class PaginationComponent implements OnInit {
@Input() pager: any;
@Output() fired = new EventEmitter<any>();
constructor() { }
ngOnInit() {
}
prev() {
let page = this.pager.current_page - 1;
if (page < 0) page = 1;
this.fired.emit({type: 'page', data: page});
}
next() {
let page = this.pager.current_page + 1;
if (page > this.pager.page_count) page = this.pager.page_count;
this.fired.emit({type: 'page', data: page});
}
}
And at parent Component or Page, should be listen the event, like:在父组件或页面上,应该监听事件,例如:
onFire(event) {
switch (event.type) {
case 'page': //pagination component fire
return this.gotoPage(event.data);
case 'edit': //list item fire
return this.editArticle(event.data);
case 'view': //list item fire
return this.viewArticle(event.data);
case 'trash': //list item fire
return this.trashArticle(event.data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.