检查按钮是否在控制器中处于活动状态

时间:2017-04-27 10:10:39

标签: angularjs ionic-framework

我有一个切换按钮,可在单击活动时更改颜色。但现在我不知道如何在控制器中获得按钮的值(活动或非活动)。我该怎么办呢?

这是错误代码:

<button class="button button-clear icon ion-star button-energized" ng-model="singleTog" ng-click="toggleButton(item.name)" ng-class="singleTog.clicked?'button-energized':'button-dark'" ></button>

这是html代码:

$scope.toggleButton = function(candidateName)
{
  $scope.singleTog.clicked=!$scope.singleTog.clicked
  if($scope.singleTog.clicked==true)
  {
    if(favoriteList.indexOf(candidateName) == -1) //does not exist in array
    {
        $scope.numbers.push(candidateName);
    }
  }
  else
  {
    if(favoriteList.indexOf(candidateName) != -1) //exists in array
    {
        var index = $scope.favoriteList.indexOf(candidateName);
        $scope.favoriteList.splice(index, 1);   
    }
  }

    alert('favorites = ' + favoriteList);
}

这是controller.js代码:

{{1}}

2 个答案:

答案 0 :(得分:0)

将emty对象定义为$scope.singleTog = {}

试试这个

$scope.singleTog = {}
$scope.toggleButton = function(candidateName)
{
  $scope.singleTog.clicked=!$scope.singleTog.clicked
  if($scope.singleTog.clicked==true)
  {
    if(favoriteList.indexOf(candidateName) == -1) //does not exist in array
    {
        $scope.numbers.push(candidateName);
    }
  }
  else
  {
    if(favoriteList.indexOf(candidateName) != -1) //exists in array
    {
        var index = $scope.favoriteList.indexOf(candidateName);
        $scope.favoriteList.splice(index, 1);   
    }
  }

    alert('favorites = ' + favoriteList);
}

答案 1 :(得分:0)

如果您只想在按钮处于活动或非活动状态时更改颜色,可以使用ng-class:

ng-class="$variableToEvaluate ? 'class-if-true' : 'class-if-false'"