2017-03-15 4 views
1

私はユーザープロファイルを作成していますが、プロファイルの所有者のみが表示する編集ボタンを表示したいと考えています。編集機能がDOMから削除されるようにng-ifを使用しようとしています。ng-ifを使用して現在のユーザーに基づいてボタンを表示するにはどうすればよいですか?

ng-ifを使用して現在のユーザーに基づいてボタンを表示するにはどうすればよいですか?私の見解では

:私のコントローラで

<button ng-if="editProfile()">EDIT</button> 

$scope.editProfile = function (canedit) { 
    var canedit = false; 
    for (var i=0;i<$scope.users.length;i++) { 
    if ($scope.users[i].id===$scope.currentUser.id) { 
     canedit = true; 
    } 
    } 
} 
+1

問題はありますか? – George

答えて

0

を回し、関数がtrueまたはfalseを返すされませんでした。リターンを追加した後、私はfor文が無関係であることを発見しました。ちょうど簡単なチェックが必要でした。このコードは正しく動作しています:

$scope.editProfile = function() { 
    if ($scope.user.id===$scope.currentUser.id) { 
    return true 
    } 
    return false 
} 
0

あなたの関数から何も戻っていません。

あなたは閉じ括弧のため

0

それを作ることができます再言及したすべての人のようtrueまたはfalse

<button ng-if="editProfile()">EDIT</button> 


$scope.editProfile = function() { 
    for (var i=0;i<$scope.users.length;i++) { 
    if ($scope.users[i].id===$scope.currentUser.id) { 
     return true 
    } 
    } 
    return false 
} 
1

あなたの関数がする必要があるの下だけ

return canedit; 

を追加し、このよう

<button ng-if="canEdit()">EDIT</button> 

とあなたのコントローラで

$scope.canEdit = function() { 
    return $scope.users.find(u=>u.id === $scope.currentUser.id) ? true : false; 
} 
関連する問題