简体   繁体   English

如何创建angular2加载指示器按钮

[英]How to create angular2 loading indicators button

How can I create simple indicators loading button with directive feature in angular2, so then can be use everywhere.also with control access in parent component? 如何在angular2中创建带有指令功能的简单指标加载按钮,以便可以在任何地方使用。还可以在父组件中进行控件访问?

// Something like this
<button loading [waitUntil]="listenToParent">Submit</button>

You can create a directive for the same like this: 您可以像这样创建指令:

@Directive({
  selector:'[loading]',
  inputs: ['waitUntil']
})
class Loading {
  private dataPromise: Promise;
  constructor(el: ElementRef){

  }

  set waitUntil(data:Promise) {
    this.dataPromise = data;
    this.dataPromise.then(function(message) {
      alert(message);
    })

  }
}

Component for the implementation of the same: 组件执行相同:

@Component({
  selector: 'my-app',
  template: `
  <h2>Hello World</h2>
  <button loading [waitUntil]="thePromise">button</button>`,
  providers: [], 
  directives: [Loading]
})
export class App implements ngAfterViewInit{
  name:any;
  thePromise: Promise ;
  constructor() {
   this.thePromise = new Promise((resolve, reject) => {
      setTimeout(function() { resolve("API call complete hide loader.");}, 1000);
    });
  }

  ngAfterViewInit(){

  }
}

From above example, you can see how a promise that was declared in the parent was passed and fulfilled in the directive, in the constructor of the directive you get the elementRef which can be used to manipulate the element, so you can show a loading symbol or disable the button element till the promise is fulfilled, once is promise is fulfilled the button can be enabled etc. depending on the requirement. 从上面的示例中,您可以看到如何在指令中传递并实现了在父级中声明的promise,在指令的构造函数中,您将获得elementRef,可用于操作元素,因此可以显示加载符号或者禁用按钮元素,直到实现承诺为止,一旦实现承诺,就可以根据需要启用按钮等。

Plnkr for the same: http://plnkr.co/edit/IptHfR?p=preview 相同的plnkr: http ://plnkr.co/edit/IptHfR?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM