I have a component that takes a single attribute. I want to populate a field in the component with a value that is derived from this attribute. I am running into the problem
One possible solution is to ave the component implement NgAttachAware
and define the attach()
method. The value of the derived field can then be set inside attach()
. I have no idea if there is a more idiomatic way to do this, but using attach()
seems to work.
Here is the code:
import 'package:angular/angular.dart';
@NgComponent(
selector: 'tokens',
templateUrl: './component.html',
cssUrl: './component.css',
publishAs: 'ctrl',
map: const {
'text' : '@text'
}
)
class TokensComponent implements NgAttachAware {
String text;
// Derived field.
List tokens = new List();
TokensComponent() {
print('inside constructor, text = $text');
}
void attach() {
print('inside attach(), text = $text'); // $text is no longer null.
text.split('').forEach((char) => tokens.add(new Token(char, false)));
}
}
class Token {
String char;
bool important;
Token(this.char, this.important);
}