最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Initialize a variable object in Angular2Javascript - Stack Overflow

programmeradmin3浏览0评论

I'm coming from Angular 1 where I'd just do this:

var options = {};
options.location = "test";
options.time = "today";

I just want to do the same thing with Angular 2 to sorta "group" my variables by adding them to objects and using interpolation to reference them in the HTML

<input [(ngModel)]="options.location" />

Now I already figured I could do this:

export class Options {
  location: string;
}

options: Options = {
  location: 'test'
};

My question: Is there a simpler way like

options: {location: string};

Thanks!

I'm coming from Angular 1 where I'd just do this:

var options = {};
options.location = "test";
options.time = "today";

I just want to do the same thing with Angular 2 to sorta "group" my variables by adding them to objects and using interpolation to reference them in the HTML

<input [(ngModel)]="options.location" />

Now I already figured I could do this:

export class Options {
  location: string;
}

options: Options = {
  location: 'test'
};

My question: Is there a simpler way like

options: {location: string};

Thanks!

Share Improve this question edited Feb 19, 2017 at 17:39 Yorick Tran asked Feb 19, 2017 at 16:52 Yorick TranYorick Tran 1331 gold badge2 silver badges9 bronze badges 2
  • Have you tried options: {location: string}; or the Angular 1 way you mentioned? – Günter Zöchbauer Commented Feb 19, 2017 at 16:53
  • 1 Yep, when I try to assign it in the constructor via this.options.location = "test"it says this.options is undefinedin the console... – Yorick Tran Commented Feb 19, 2017 at 17:12
Add a comment  | 

5 Answers 5

Reset to default 6

Typescript can be considered as extended JavaScript. Any valid JavaScript code is valid Typescript code.

The code you wrote in your question :

var options = {};
options.location = "test";
options.time = "today";

...is perfectly legal (but not optimal).

The thing is that simply declaring options the way you do in the component won't make it visible from the template. options must be a public member of the Component class. Your Component class should look like

@Component({
  template: `
<input [(ngModel)]="options.location" />
`})
export class FormsPage {
  options = {location :"test", time : "today" }
}

Note that if I were you, I would read Typescript documentation (and even JavaScript one) in addition to Angular2's one. It would help you to better understand how handle it.

Found it myself -

this.options = {location: "test"};

instead of

this.options.location = "test"

Not sure why the second version does not work. If you have insight please share in the comments!

To initialize a ngModel value in angular , Create a class Options like this:

Options.ts

export class Options {
  location: string;
  time: string;
  constructor(location: string, time: string) {

  }
}

Or

export class Options {
  location: string;
  time: string;
  constructor(location: string, time: string) {
    this.location = location;
    this.time = time;
  }
}

HTML:

<input type="text" name="location" [(ngModel)]="options.location" #location="ngModel" required/>

<input type="text" name="time" [(ngModel)]="options.time" #time="ngModel" required/>

And in your component initialize options like this:

options = new Options('','');

NOTE: if you create Options class like this:

   export class Options {
      constructor(location: string, time: string) {

      }
    }

It will work with ng serve but NOT with ng build --prod as AOT(Ahead-of-Time) compilation gives error:

ERROR in ng:/xxxx/x.component.html (67,74): Property 'location' does not exist on type 'Options'.
ERROR in ng:/xxxx/x.component.html (72,72): Property 'time' does not exist on type 'Options'.

If you initialize options in component in this way

options={};

will also gives same error.

You can do it this way:

options: Options = {
  location: 'test'
} as Options;

you can do like this options:

Options = new Options ({ location :"test", time : "today"  });
发布评论

评论列表(0)

  1. 暂无评论