【问题标题】:AngularJS Get Attribute ValueAngularJS 获取属性值
【发布时间】:2012-12-10 13:30:45
【问题描述】:

我的控制器中有以下角度对象

$scope.students = [{:id => 1, :state => 'active'}, {:id => 2, :state => 'suspended'}]

如何获取所有状态为活动的学生的 ID?谁能帮我解决这个问题?

【问题讨论】:

    标签: angularjs javascript-framework


    【解决方案1】:

    有多种方法,但我能想到的最简单的方法是在您的作用域中添加一个 activeStudent 函数,如下所示:

    $scope.activeStudent = function(){
      return _.filter($scope.students, function(s){
        return s.state == "active";
      });
    }
    

    在你看来,你会在绑定中使用 activeStudent()。

    【讨论】:

    • 非常感谢 Schmurfy!有没有更好的方法来获取所有学生的 id 而不是循环和收集 id?喜欢 $scope.students.collect(&:id) 再次感谢!
    • 我在 angularjs 中使用下划线,对此我很满意(我在示例中使用过),您可以查看 underscorejs.org/#mapunderscorejs.org/#pluck
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多