2014-01-13 98 views
1

我試圖創建具有兩種狀態addadded這兩個州都在我的解決方案可見點擊功能,但我只想要一個狀態到一次顯示。到目前爲止,我的邏輯是以下面的方式來構造它。演示時,我在班級被激活的項目點擊如何,我無法刪除類我注意到。 DemoNG-點擊添加和刪除類

<div ng-repeat= "fav in fav"> 
    <div ng-click="select(fav)" ng-class="favClass(fav)" class="panel-controls panel-controls__item"> 
    {{fav}} 
</div> 

    var app = angular.module("myApp", []); 
    app.controller("favouriteBtn", function ($scope){ 

    $scope.fav = ['add', 'added']; 
    $scope.selected = $scope.fav[0]; 

    $scope.select = function(fav) { 
     $scope.selected = fav; 
     }; 

    $scope.favClass = function(fav){ 
     return fav === $scope.selected ? 'active' : underfined; 
    }; 
    }); 

回答

0

只是一個猜測:

ng-class="{active: fav == selected}" 

,或者如果你只是想改變顯示按鈕:

ng-show="fav == selected" 

ng-show="fav != selected" 
+0

它工作在一定程度上,但它不同時顯示一個狀態。例如在我的演示中,我有兩個框,我試圖只顯示一個框。 – NewKidOnTheBlock

+0

那麼你應該嘗試NG-秀=「最愛==選擇」或NG秀=「最愛!=選擇」 – michael

+0

你不介意的答案寫這個嗎? – NewKidOnTheBlock

0

我會做的邁克爾說。

在視圖:

<div class="{{selected}}"> 
    <div ng-show="selected == 'add'"> 
    <button ng-click="toggleButton()">Button1</button> 
    </div> 
    <div ng-show="selected == 'added'"> 
    <button class='disabled'>Button2</button> 
    </div> 
</div> 

在控制器:

angular.module('myApp').controller('ButtonCtrl', 
[ 
    '$scope', 
    function($scope){ 
    $scope.fav = ['add', 'added']; 
    $scope.selected = fav[0]; 

    $scope.toggleButton = function(){ 
     $scope.selected = fav[1]; 
    }; 
    } 
]);