Как обновить представление после изменения в angular2 после запуска прослушивателя событий Google
Я пытаюсь обновить представление после запуска прослушивателя событий. Однако изменение не обнаруживается и не обновляется до тех пор, пока не будет обнаружено другое изменение.
import {
Component, View, bootstrap
} from 'angular2/angular2';
@Component({
selector: 'app'
})
@View({
template: '{{keyword}} <input id="keyword" /><br/><span (click)="update()">{{click}}</span>'
})
class App {
keyword;
autocomplete;
click;
constructor() {
var _this = this;
var input = (document.getElementById('keyword'));
this.autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(this.autocomplete, 'place_changed', function(){
console.log('place change');
_this.keyword = "updated text";
_this.click = "not clicked";
});
this.keyword = "original text";
this.click = "click me after selection is made to force change";
}
update() {
console.log("click");
this.click = "clicked";
}
}
bootstrap(App);
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://jspm.io/system.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.30/angular2.dev.js"></script>
<script src="https://maps.googleapis.com/maps/api/js??v=3.exp&libraries=places"></script>
</head>
<body>
<app></app>
<script>
System.import('main');
</script>
</body>
</html>
Ответы
Ответ 1
@jhadesdev привел меня в правильном направлении, но вместо zone.runOutsideAngular()
мне понадобился zone.run()
. Вот полный код javascript, который работал:
import {
Component, View, bootstrap, NgZone
} from 'angular2/angular2';
@Component({
selector: 'app'
})
@View({
template: '{{keyword}} <input id="keyword" /><br/><span (click)="update()">{{click}}</span>'
})
class App {
keyword;
autocomplete;
click;
zone: NgZone;
constructor(zone:NgZone) {
this.zone = zone;
var input = (document.getElementById('keyword'));
this.autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(this.autocomplete, 'place_changed', ()=>{
this.zone.run(() => {
console.log('place change');
this.keyword = "updated text";
this.click = "not clicked";
});
});
this.keyword = "original text";
this.click = "click me after selection is made to force change";
}
update() {
console.log("click");
this.click = "clicked";
}
}
bootstrap(App);
Ответ 2
Я думаю, проблема заключается в том, что поле выбора автозаполнения абсолютно позиционируется в конце элемента тела страницы, и поэтому вне зоны, которая отслеживается Angular (которая находится внутри div, где app
есть).
Чтобы изменить это, выполните следующие действия:
-
добавьте CSS, чтобы поместить поле выбора относительно (используя !important
), чтобы он оставался внутри зоны
-
запускайте обнаружение изменений вручную, используя NgZone:
zone.run(() => {
console.log('place change');
_this.keyword = "updated text";
_this.click = "not clicked";
});