【问题标题】:Complex Angular script复杂的 Angular 脚本
【发布时间】:2017-03-16 21:17:19
【问题描述】:

我正在创建基于 ng-repeat 的 div。我正在从数据库中获取数据。数据如下:

[
    {
        id:1,
        name:item1,
        category: [catA, catB, catC,]
    },
    {
        id:2,
        name:item2,
        category: [catB]
    },
    {
        id:3,
        name:item3,
        category: [catA, catB]
    }
]

此数据进入范围。所以:

myApp.controller("myCtrl", function($scope, $http){

    var cat = "catA";

    $http.get("/getdata", function(response){
        if(response.data){
            $scope.items = response.data;

            if(response.data.category==cat){    // Check if the var cat is present in the array. How to do this?
                //set ng-class for the button in the div to 'active'. How to do this?
            }

        } else {
            $scope.message = "Nothing returned.";
        }
    });

    $scope.send = function(n){
        $scope.isActive = !$scope.isActive;
   }
});

我正在使用 ng-repeat :

<div ng-repeat="item in items" class="myclass">
        {{item.name}}
        <button class="myRight" ng-class="{'active': isActive}" ng-click="send(item.id)"> click </button>
</div>

活动类:

.active { background: #FF735C; color:white;}

因此,根据数据中接收到的category数组中存在的var cat,确定按钮的类别。如果单击该按钮,则该类将切换。我不能使用 $index 作为按钮来传递 var,就好像数据被过滤一样,没有呈现正确的索引。

我可以使用 ng-class 吗?有没有其他更简单的方法来实现这一点?

请帮忙。非常感谢。

【问题讨论】:

  • 所以要确认一下,如果项目有catA作为类别,按钮应该是active,而不是active,一旦按下按钮?
  • 是的。确切地。如果一个项目没有catAitem2,那么这个div 中的按钮应该没有活动类。
  • 如果点击了一个活动按钮,它是否会变成“非活动”并且无法再次点击?
  • 谢谢。不,它只是一个类。该按钮可再次单击。只有类切换。我在问题中添加了课程。

标签: angularjs angularjs-ng-repeat


【解决方案1】:

希望这能解决您的问题,ng-class 绝对是正确的方法。

// our store of active buttons/items
$scope.buttonActive = {};

$http.get("/getdata", function(response)
{
    if (response.data)
    {
        angular.forEach(response.data, function (item)
        {
            // create a list of active items based on if it has a category
            $scope.buttonActive[item.id] = item.category.indexOf(cat) !== -1;
        });

        $scope.items = response.data;
    }
    else
    {
        $scope.message = "Nothing returned.";
    }
});

/**
 * @param id
 */
$scope.send = function (id)
{
    // toggle the boolean value for this button/item's state
    $scope.buttonActive[id] = !$scope.buttonActive[id];
}

在模板中:

<div ng-repeat="item in items" class="myclass">
    {{ item.name }}
    <button class="myRight" ng-class="{ 'active': buttonActive[item.id] }" ng-click="send(item.id)">click</button>
</div>

更新

if 语句本质上是通过将一个或多个条件评估为布尔值来工作的。

if (true)
    doSomething();

所以我在那条线上所做的是:

// if item.categories array contains the category in 'cat' variable
if (item.category.indexOf(cat) !== -1)
    $scope.buttonActive[item.id] = true;
else
    $scope.buttonActive[item.id] = false;

但是看到我根据另一个布尔值分配一个布尔值,我不妨只使用初始条件的结果。

var isInArray = item.category.indexOf(cat) !== -1;

$scope.buttonActive[item.id] = isInArray;

或者只是:

$scope.buttonActive[item.id] = item.category.indexOf(cat) !== -1;

【讨论】:

  • 这个魔法叫什么? $scope.buttonActive[item.id] = item.category.indexOf(cat) !== -1我在哪里可以了解更多信息?
猜你喜欢
  • 2012-04-28
  • 2014-06-10
  • 1970-01-01
  • 2011-09-22
  • 2010-10-23
  • 2014-08-06
  • 2018-01-08
  • 1970-01-01
  • 2016-08-19
相关资源
最近更新 更多