2017-03-02 54 views
0

所以我有一個用戶關注的人員列表。我想要讓當前用戶查看訪客用戶的關注。跟隨當前用戶的按鈕被刪除。這是我的嘗試。如何動態地爲currentuser隱藏元素?

編輯:當我運行這樣的代碼時,所有用戶都會顯示按鈕並顯示以下內容。它不會隱藏當前用戶。

HTML代碼:

<ion-header> 

    <ion-navbar> 
    <ion-title>Follow</ion-title> 
    </ion-navbar> 

</ion-header> 


<ion-content> 
<ion-list> 
    <button ion-item *ngFor="let pic of pics" (click)="viewGuest(pic.user)"> 
    <ion-thumbnail item-left> 
     <img src="{{pic.avi['url']}}"> 
    </ion-thumbnail> 
    <h2>{{pic.additional}}</h2> 
    <p>{{pic.user}}</p> 
    <div [ngSwitch]="value" item-right> 
     <span *ngSwitchCase="false"> 
     <span [ngSwitch]="connect"> 
      <span *ngSwitchCase="false"> 
      <button ion-button>Follow</button> 
      </span> 
      <span *ngSwitchDefault> 
      <button ion-button>Following</button> 
      </span> 
     </span> 
     </span> 
     <span *ngSwitchDefault></span> 
    </div> 
    </button> 
</ion-list> 
</ion-content> 

TS編碼:

import { Component } from '@angular/core'; 
import { NavController, NavParams } from 'ionic-angular'; 
import { GuestPage} from '../guest/guest' 
var Parse = require('parse'); 

@Component({ 
    selector: 'page-follow', 
    templateUrl: 'follow.html' 
}) 
export class FollowPage { 

    follows = [] 
    pics = [] 
    parameter1 = '' 
    passedUser = '' 
    value = false 
    connection = false 

    constructor(public navCtrl: NavController, public navParams: NavParams) { 
    Parse.initialize('hidden','unused', "hidden"); 
    Parse.serverURL = 'hidden'; 
    this.parameter1 = navParams.get('param1') 
    this.passedUser = navParams.get('userParam') 
    } 

    ionViewWillEnter(){ 
    if (this.parameter1 == "Followers"){ 
    this.initializeFollowers() 
    } else{ 
    this.initializeFollowing() 
    } 

    if (this.follows == Parse.User.current().get("username")){ 
    this.value = true 
    } else{ 
    this.value = false 
    } 




    console.log(this.parameter1); 
    } 

    ionViewDidLeave(){ 
    this.follows.length = 0 
    this.pics.length = 0 
    this.parameter1='' 
    } 

    initializeFollowers() { 
    var this_ref = this 
    var currentUser = Parse.User.current().get("username"); 
    var Follower = Parse.Object.extend("Follow") 
    var query = new Parse.Query(Follower); 
    query.equalTo("following", this.passedUser); 
    query.find({ 
     success: function(follower) { 
     for (var i = 0; i < follower.length; i++) { 
     this_ref.follows.push(follower[i].get("follower")), 
     console.log(this_ref.follows); 
     } 
     var picture = Parse.Object.extend("Profile") 
     var query = new Parse.Query(picture); 
     query.containedIn("user", this_ref.follows); 
     query.find({ 
      success: function(picture) { 
      for (var i = 0; i < picture.length; i++) { 
      this_ref.pics.push(picture[i].toJSON()), 
      console.log(this_ref.pics); 
      } 
      } 
     }); 
     } 
    }); 
    } 

    initializeFollowing() { 
    var this_ref = this 
    var currentUser = Parse.User.current().get("username"); 
    var Following = Parse.Object.extend("Follow") 
    var query = new Parse.Query(Following); 
    query.equalTo("follower", this.passedUser); 
    query.find({ 
     success: function(following) { 
     for (var i = 0; i < following.length; i++) { 
     this_ref.follows.push(following[i].get("following")), 
     console.log(this_ref.follows); 
     } 
     var picture = Parse.Object.extend("Profile") 
     var query = new Parse.Query(picture); 
     query.containedIn("user", this_ref.follows); 
     query.find({ 
      success: function(picture) { 
      for (var i = 0; i < picture.length; i++) { 
      this_ref.pics.push(picture[i].toJSON()), 
      console.log(this_ref.pics); 
      } 
      } 
     }); 
     } 
    }); 
    } 

    viewGuest(pic: string){ 
    this.navCtrl.push(GuestPage, { userParam: pic}) 
    console.log(pic); 

    } 

} 
+0

任何理由交換機,而不是'* ngIf'?它會減少你的容器......以及代碼發生了什麼?任何問題? –

+0

我將解釋編輯中會發生什麼。我使用開關,因爲它實際上並沒有被隱藏,但我可以很容易地將它改爲if。 –

+0

所以..你正在嘗試的是如果* loggedIn用戶*在下面的列表中的按鈕不應該顯示。 –

回答

1

this.follows == Parse.User.current().get("username") 這裏你的值以與陣列這將是假的比較。 我會做的是:

有一個類變量currentUser在ionViewWillEnter

ionViewWillEnter(){ 
    this.currentUser=Parse.User.current().get("username"); 
} 

在HTML而不是開關的情況下設定值使用,如果:

<div item-right> 
     <span *ngIf="pic.user!=currentUser"> 
     <span [ngSwitch]="connect"> 
      <span *ngSwitchCase="false"> 
      <button ion-button>Follow</button> 
      </span> 
      <span *ngSwitchDefault> 
      <button ion-button>Following</button> 
      </span> 
     </span> 
     </span> 
    </div> 

還建議您使用*ngIf而不是其他布爾條件的開關情況。

+0

這工作完美,我非常感謝所有的幫助。我會用同樣的方法進行跟隨/以下的感謝 –