2012-12-10 150 views
0

我在我的控制下角對象AngularJS獲取屬性值

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

我怎樣才能讓所有的學生,其狀態是活躍的ID?任何人都可以幫助我嗎?

回答

1

有多種方式,但我能想到的最簡單的就是在你的範圍內添加activeStudent功能,這樣的事情:

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

和你的意見,你會在你的綁定使用activeStudent()。

+0

非常感謝Schmurfy!有沒有更好的方式來獲得所有學生的ID而不是循環和收集ID?像** $ scope.students.collect(&:id)**再次感謝! –

+0

我使用下劃線和angularjs,我對它很滿意(我在例子中使用了它),你可以看看http://underscorejs.org/#map和http://underscorejs.org/#pluck。 – Schmurfy