首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ionic 2和角2中不更新变量变化的视图

在Ionic 2和角2中不更新变量变化的视图
EN

Stack Overflow用户
提问于 2017-05-11 03:13:44
回答 1查看 1.4K关注 0票数 0

我刚开始使用角度2,所以我可能不知道出了什么问题。但是,我有一个*ngIf,如果变量为false,它应该显示一个按钮,如果变量为true,则不显示该按钮。

但是,一旦我将变量更新为true,按钮就不会消失。任何帮助都是非常感谢的。

代码:

HTML

代码语言:javascript
复制
ion-slide>
<h1> Uber Button </h1>
<button ion-button color="dark" *ngIf="!uberSettings?.uberActivated" (click)="userSettings.connectUber()">Activate Uber</button>
</ion-slide>

构成部分:

代码语言:javascript
复制
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HomePage } from '../home/home';
import { UserSettings} from '../../providers/user-settings'


@Component({
 selector: 'page-setup',
 templateUrl: 'set-up.html'
})

export class SetUpPage {

  constructor(public navCtrl: NavController, private userSettings: UserSettings) {

 }

  goToHome(){
   this.navCtrl.setRoot(HomePage);
 }



}

服务:

代码语言:javascript
复制
declare
var window: any;


@Injectable()
export class UserSettings {

authorizationCode: string;
uberData: any;

uberSettings: UberSettings;
result: any;




constructor(public http: Http, private platform: Platform, public storage: Storage) {

    console.log('called uberSettings Constructor');

    storage.ready().then(() => {

        storage.get('uberSettings').then((val) => {

                console.log('the val is' + val);
                if (val == null) {
                    console.log('val equaled null');
                    this.uberSettings = {
                        buttonOn: false,
                        uberActivated: false,
                        token: null,
                        refreshToken: null,
                        long: null,
                        lat: null,
                    }

                    console.log(this.uberSettings);

                    storage.set('uberSettings', this.uberSettings);

                    // this.uberSettings.uberActivated = true; 
                    // // this.uberSettings.token= val.token;
                } else {
                    console.log("there was a value for Val");
                    this.uberSettings = val
                    console.log(this.uberSettings);
                }
            })

            .catch(err => {
                console.log('we dont have an uber token yet' + JSON.stringify(err));
            });


    });




}

public connectUber() {
    this.platform.ready().then(() => {
        this.uberLogin().then(success => {
            this.authorizationCode = success;
            console.log(this.authorizationCode);
            this.token()

        }, (error) => {
            alert(error);
        });
    });
}


token() {

    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    // let body = 'client_id=3ibytPcye4w3_-0txknyO7ptD-MfFMkn&client_secret=2Kp8O54mGvlOoBm6IPX_0gKBmJ_mODSo7FqPbbA9&redirect_uri=http://localhost:8100/callback&grant_type=authorization_code&code=' + this.authorizationCode

    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('client_id', <CLIENT ID>);
    urlSearchParams.append('client_secret', <CLIENT SECRET>);
    urlSearchParams.append('redirect_uri', 'http://localhost:8100/callback');
    urlSearchParams.append('grant_type', 'authorization_code');
    urlSearchParams.append('code', this.authorizationCode);
    let body = urlSearchParams.toString()


    return this.http.post(`https://login.uber.com/oauth/v2/token`, body, {
            headers: headers
        })
        .map(response => response.json())
        .subscribe(result => {

            this.result = result
            console.log(this.result);
            this.uberSettings.token = this.result.access_token;
            this.uberSettings.refreshToken = this.result.refresh_token;
            this.uberSettings.uberActivated = true;
            this.uberSettings.buttonOn = true;
            console.log(this.uberSettings);

        });

    public uberLogin(): Promise < any > {
        return new Promise(function(resolve, reject) {
            var browserRef = window.cordova.InAppBrowser.open("https://login.uber.com/oauth/v2/authorize?client_id=3ibytPcye4w3_-0txknyO7ptD-MfFMkn&response_type=code", "_blank", "location=no,clearsessioncache=yes,clearcache=yes");
            browserRef.addEventListener("loadstart", (event) => {
                if ((event.url).indexOf("http://localhost:8100/callback") === 0) {
                    browserRef.removeEventListener("exit", (event) => {});
                    browserRef.close();

                    var responseParameters = ((event.url).split("code="));

                    var parsedResponse = responseParameters[1].split("#");

                    if (parsedResponse[0] !== undefined && parsedResponse[0] !== null) {
                        resolve(parsedResponse[0]);
                    } else {
                        reject("Problem authenticating with Uber");

                    }

                }
            });
            browserRef.addEventListener("exit", function(event) {
                reject("The Uber sign in flow was canceled");
            });
        });
    }

}

因此,如果您查看服务代码,我将在调用token()时更新this.uberSettings,并且控制台显示this.uberSettings.uberActivated已更改为true。但是,我不明白为什么那个按钮还会显示出来。任何帮助都将不胜感激。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-11 03:29:56

您的ngIf没有引用您的服务。我想你有一个错误:)

代码语言:javascript
复制
*ngIf="!userSettings.uberSettings?.uberActivated" 

而不是

代码语言:javascript
复制
*ngIf="!uberSettings?.uberActivated" 
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43905921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档