【问题标题】:value not updating in the view after data update , angular2 and typescript数据更新,角度2和打字稿后视图中的值未更新
【发布时间】:2016-07-23 02:22:36
【问题描述】:

这是我的ngOnInit 函数

 instance.input = document.getElementById('google_places_ac');
        autocomplete = new google.maps.places.Autocomplete(instance.input, { types: ['(cities)']});
        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            var place = autocomplete.getPlace();
            instance.setValue(place.address_components[3].long_name, place.address_components[2].long_name, place.address_components[1].long_name);

         });

这是我的setValue() 函数

public setValue(a,b,c) {
    this.coutnry = a;
    this.state = b;
    this.city = c;
    this.sharedService.country = this.coutnry;
    this.sharedService.city = this.city;
    this.sharedService.state = this.state;
    console.log(a, b, c);
}

下面是我的 html 模板,用于显示和获取值

 <input id="google_places_ac" attr.state="{{state}}" attr.country="{{coutnry}}" name="google_places_ac" type="text" value="{{city}}" class="form-control" />

问题是我的 html 属性在调用 place_changed 函数后没有得到更新,我需要做些什么来获得更新的值吗?

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    您应该通过在[] 中包装属性来使用属性绑定

    <input id="google_places_ac" [attr.state]="state" [attr.country]="coutnry" 
     name="google_places_ac" type="text" [value]="city" class="form-control" />
    

    另外,你在写country 时写错了,就像你写coutnry 一样,而是在两个地方写错了。

    this.sharedService.country = this.coutnry; //in constructor
    
    attr.country="{{coutnry}}" //second place
    

    【讨论】:

    • 当我这样做时,我得到了一个很长的错误得到插值({{}}),其中表达式应该在 [{{state}}] 的第 0 列,这适用于所有属性。我在 ngOnInit 中调用这个函数
    • @Ironsun 您确定将代码更改为我更新的答案吗?
    • Eagle eyes@PankajParkar
    • 好的,我使用的是 {{}},但现在没有收到错误,但值仍然没有更新
    • @Ironsun 您能否调试并确认setValue 函数正确设置了state 和其他值?
    【解决方案2】:

    你可以查看这个 angular2 官方文档来设置属性,

    备忘单 - https://angular.io/docs/ts/latest/cheatsheet.html

    <input id="google_places_ac" [attr.state]="state" [attr.country]="coutnry"
    name="google_places_ac" type="text" value="{{city}}" class="form-control" />
    

    更新

    import { Component, ChangeDetectorRef } from 'angular2/core';
    
    constructor(private cdr:ChangeDetectorRef) {
    }
    public setValue(a,b,c) {
        this.coutnry = a;
        this.state = b;
        this.city = c;
        this.sharedService.country = this.coutnry;
        this.sharedService.city = this.city;
        this.sharedService.state = this.state;
        console.log(a, b, c);
        this.cdr.detectChanges();
    }
    

    【讨论】:

    • 我将 console.log 更改为 console.log(this.coutnry, this.state, this.city);这显示了正确的值,但视图没有更新
    • 这可能是 changeDetection 问题或 ngZone 问题。检查我更新的答案。
    • 是的,伙计...非常感谢您用 changedetection 启发我 :)
    • 嘿,我刚刚遇到了一个后续问题,我如何检测父元素对子元素的更改,事件是从父组件触发的,而子组件在路由出口中从共享服务获取值,可以你帮我吗?
    • @micronyks 没有查看代码..因为它已被 A2 的outer context 解雇.. +1 用于更改检测的核心问题
    猜你喜欢
    • 2020-12-17
    • 2017-08-14
    • 2021-09-09
    • 2015-06-26
    • 1970-01-01
    • 2016-08-31
    • 2016-01-19
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多