[Angular] Use Angular components in AngularJS applications with Angular Elements

When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular Elements is one of them. In this lesson we learn how to integrate an Angular Element into an AngularJS (v1.x) application. We will leverage some features release in the latest AngularJS 1.7.3 that make it fully compatible with custom elements.

 

Angular Element:

复制代码
import {
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter,
  AfterViewInit,
  ElementRef,
  Attribute,
  AfterContentInit,
  ChangeDetectorRef
} from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  // tslint:disable-next-line:component-selector
  selector: 'feedback-form',
  templateUrl: './feedback-form.component.html',
  styleUrls: ['./feedback-form.component.scss']
})
export class FeedbackFormComponent implements OnInit {
  feedbackForm: FormGroup;
  _name;

  @Input()
  set name(val) {
    this._name = val;

    this.feedbackForm.patchValue({
      name: val
    });
  }
  get name() {
    return this._name;
  }

  @Output()
  feedbackSubmit = new EventEmitter();

  constructor() {}

  ngOnInit() {
    this.feedbackForm = new FormGroup({
      name: new FormControl(this.name),
      feedback: new FormControl('')
    });
  }

  onSubmit({ value, valid }) {
    if (valid) {
      this.feedbackSubmit.next(value);
    }
  }
}
复制代码

 

Using in AngularJS:

复制代码
    
const appModule = angular.module('myApp', []);

appModule.component('myApp', {
  template: `
    <h1>AngularJS <3 Angular</h1>
    <feedback-form ng-prop-name="$ctrl.name" ng-on-feedback_submit="$ctrl.onFeedbackSubmit($event)"></feedback-form>
  `,
  controller: function() {
    this.name = 'Juri';

    this.onFeedbackSubmit = ev => {
      console.log('Got ', ev.detail);
    };
  }
});

angular.element(function() {
  angular.bootstrap(document, ['myApp']);
});
复制代码

Here the important things is to know how to listen to the event and passing the prop:

ng-prop-<input_name>
ng-on-<output_name>

 

posted @   Zhentiw  阅读(316)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-03-22 [Angular] Updating and resetting FormGroups and FormControls
2017-03-22 [Angular] Subscribing to the valueChanges Observable
2016-03-22 [Angular 2] Build a select dropdown with *ngFor in Angular 2
2016-03-22 [Angular 2] Refactoring mutations to enforce immutable data in Angular 2
点击右上角即可分享
微信分享提示