Skip to content

Instantly share code, notes, and snippets.

@xiongxin
Last active October 9, 2019 18:52
Show Gist options
  • Save xiongxin/fac3a2889d46453cdfa03f08a3fff599 to your computer and use it in GitHub Desktop.
Save xiongxin/fac3a2889d46453cdfa03f08a3fff599 to your computer and use it in GitHub Desktop.
Angular Dart ViewChild ContentChild example
import 'dart:html';
import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';
@Component(
selector: 'child-directive',
template: '<p>child</p>'
)
class ChildDirective {
doSomething() {}
}
@Component(
selector: 'tab',
template: '''
<p>{{title}}</p>
''',
)
class TabComponent {
@Input() String title;
}
@Component(
selector: 'tabs',
template: '''
<ng-content></ng-content>
''',
)
class TabsComponent implements AfterContentInit {
@ContentChildren(TabComponent) QueryList<TabComponent> tabs;
ngAfterContentInit() {
tabs.forEach((TabComponent tab) {
window.alert(tab.title);
});
}
}
@Component(
selector: 'binding-syntax',
host: const {
'role': 'tabpanel',
},
template: '''
<b #bs>Binding Syntax</b>
<p on-click="pOnClick()">click</p>
<tabs>
<tab bind-title="one"></tab>
<tab bind-title="two"></tab>
</tabs>
<child-cmp></child-cmp>
<child-cmp></child-cmp>
''',
directives: const [CORE_DIRECTIVES, formDirectives,ChildDirective, TabsComponent,TabComponent]
)
class BindingSyntaxComponent implements AfterViewInit {
var name = 'Angular';
var one = 'one';
var two = 'two';
void pOnClick() {
print("pOnClick");
}
@ContentChildren(TabComponent) QueryList<TabComponent> tabs;
@ViewChild("bs")
ElementRef element;
@ContentChild(ChildDirective)
QueryList<ChildDirective> contentChild;
@override
ngAfterViewInit() {
// TODO: implement ngAfterViewInit
window.console.dir((element.nativeElement as Element).innerHtml);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment