简体   繁体   中英

Ember: how to set correctly Controller property

I updated a property of Controller and attempted to use it in a console.log() . In the HTML page I see the updated value of the property, but console.log() still shows me the old value. What did I do wrong? How to get the new value in console.log()?

Details are in http://emberjs.jsbin.com/AHiVeGe/3/edit

First, you have to use 'get' on a property. Instead of:

console.log(this.controllerFor('index').latitude);

write

console.log(this.controllerFor('index').get('latitude'));

Second, navigator.geolocation.getCurrentPosition runs asynchronously and returns immediately, so your console.log is running before latitude is set, but after it returns.

Third, the model hook runs before the controller is set up.

Fourth, the model has to be an object.

I wasn't quite sure what you wanted to do, but in http://emberjs.jsbin.com/AHiVeGe/6/edit I change a property on the model after the latitude is set and console.log it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM