angular - How to create object as service member? -
i try access main template (app.tpl) properties of member object (appdriver) provided service (appdriverservice). problem... : during instantiation of appdriverservice, return of appdriver constructor undefined :/
here files, if me.
appdriver.ts
export class appdriver { public path: string; public content: string; constructor (){ this.path = ''; this.content = ''; } }
appdriverservice.ts
import {appdriver} 'app/appdriver.ts'; export class appdriverservice { public appdriver : appdriver; constructor(){ appdriver = new appdriver(); console.log(this.appdriver); // here problem // this.appdriver = undefined ?????? } public getappdriver(): appdriver { return this.appdriver; } }
app.ts
import {component, view, bootstrap, form_directives} 'angular2/angular2'; import {appdriverservice} 'app/services/appdriver-service.ts'; import {appdriver} 'app/appdriver.ts'; @component({ selector: 'my-app', templateurl: 'app/app.tpl', directives: [form_directives] }) class myappcomponent { appdriver: appdriver; constructor(appdriverservice : appdriverservice) { this.appdriver = appdriverservice.getappdriver(); } onchange ($event : event) { var file = $event.target.files[0]; this.appdriver.path = file.name; var reader = new filereader(); reader.onload = function(e){ this.appdriver.content = e.target.result; }.bind(this); reader.readastext(file); } } bootstrap(myappcomponent, [appdriverservice]);
app.tpl
<input type="file" (change)="onchange($event)"> <p>{{appdriver.path}}</p> <textarea>{{appdriver.content}}</textarea>
Comments
Post a Comment