3

Here issue is how to set the input model value from a service in Angular 2.

Here is my sample code:
component:

//our root app component
import {Component} from 'angular2/core'
import {Service} from './service'

@Component({
  selector: 'my-directive',
  providers: [],
  template: `<input [(ngModel)]="abc">`,
  directives: []
})
export class Directive {
  constructor(public service: Service) {
    this.abc = this.service.value;
  }
}

service:

//our root app component import {Injectable} from 'angular2/core'

@Injectable()
export class Service {
  constructor() {
    this.value = "service"
  }
  abcFun(){
    // need to update value from here
  }
}

plunkr here

5
  • Is _abcService returning what you want to store inside this.modal? Commented Aug 2, 2016 at 18:01
  • i need to update model in abcservice in getList method. Commented Aug 2, 2016 at 18:03
  • Please provide the service and component code. Commented Aug 2, 2016 at 18:03
  • We have one specific scenario, that's why we need to update value from that service it's self. Commented Aug 2, 2016 at 18:12
  • it looks like you are missing import or directive. the [(ngModel)] screw everything Commented Aug 2, 2016 at 20:37

2 Answers 2

4

Try this :

component.html

<input type="text" [(ngModel)]="abc">

component.ts

private abc: string;

constructor(
        public homeService: HomeService
    ) { }
ngOnInit() {
        this.abc = this.homeService.value;
    }

component.service.ts

public value: string;

constructor(private http: Http) { 
        this.value = "Hello world";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you have different instances of the value in Service and Controller. If you change value in the service, you only change the instance there, the instance of the controller is still the old one. It's the same the other way around. You can solve this by storing the string in an object like this.

@Injectable()
export class Service {
  data = { value: '' };
  constructor() {
    this.data.value = "service"
  }
}

@Component({
  selector: 'my-directive',
  providers: [],
  template: `<input [(ngModel)]="abc.value">`,
  directives: []
})
export class Directive {
  constructor(public service: Service) {
    this.abc = this.service.data;
  }
}

Another solution is to setup communication with observables as shown here.

@Injectable()
export class Service {
  valueSource = new Subject<string>();
  value$ = this.valueSource.asObservable();
  updateValue(value: string) {
    this.valueSource.next(value);
  }
}

@Component({
  selector: 'my-directive',
  providers: [],
  template: `<input [(ngModel)]="value">`,
  directives: []
})
export class Directive {
  value: string;
  constructor(public service: Service) {
    this.service.value$.subscribe(value => this.value = value);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.