0

Is there any way to get the string "myModel" in my component?

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<input type="text" [(ngModel)]="myModel"/>`
})
export class AppComponent { // how can I access "myModel" here? }
Shackles
  • 1,264
  • 1
  • 19
  • 40
  • 1
    This would be pretty basic... maybe check out tutorials?? ;) https://angular.io/docs/ts/latest/tutorial/toh-pt1.html – AT82 Mar 22 '17 at 20:18
  • I don't mean the value of myModel but the string literal "myModel". – Shackles Mar 23 '17 at 22:05

2 Answers2

0

Simply access it with

this.myModel

and declare it before.

myModel:string;
sascha10000
  • 1,245
  • 1
  • 10
  • 22
  • No sorry, I don't mean the value of myModel but the string literal "myModel". – Shackles Mar 23 '17 at 22:04
  • I don't understand your use-case? ngModel binds the value of the input to the given variable in your case myModel. Please clearify your question. It seems like you don't understand what the binding is for... Do you wan't to pass a value to your component? – sascha10000 Mar 25 '17 at 01:50
  • use case would be simple. assuming we have an address component and the main component does bind to: model.HomeAddress and model.PostalAddress (via property or ngModel - depends on implementation), then i'd like to know the binding expression, so i'd like to get either "model.HomeAddress" or the "model.PostalAddress" string. if you don't get why, don't ask, if you don't know how to do, don't answer. simply there's a reasonable UC (based on composition) where this might be needed. – baHI Dec 11 '17 at 18:29
  • just like it's possible to translate/evaluate an expression: https://stackoverflow.com/questions/39765063/angular-2-binding-expression-in-string-ng2-translate it also makes sense to grab that expression :D – baHI Dec 11 '17 at 18:29
  • Okay, I must admit you stated that in your question, but to me it was a bit obscure, regarding to the use cases I could think of, but that doesn't matter. If you still need a possible solution the ViewChild-Decorator may help https://angular.io/api/core/ViewChild . I couldn't test it because it is a bit late. – sascha10000 Dec 12 '17 at 00:38
0

The nearest i could come to NgModel was in the ctor via injector.view - but need to do some changes based on http://blog.rangle.io/angular-2-ngmodel-and-custom-form-components/ to see if i'll be able to get those expressions. should anyone get an answer sooner than me, please, post it here. thanx.

baHI
  • 1,510
  • 15
  • 20
  • tried also the component with ngModel binding (see url above), yet no luck to grab that expression – baHI Dec 12 '17 at 06:11