2017-02-18 64 views
0

我完全相信我會以錯誤的方式去解決這個問題,但是看到我與angularjs有多新,我無法弄清楚如何完成我想要的。我有一組使用ng-repeat的用戶,但我無法弄清楚如何通過範圍變量userId進行過濾。 User id: <name>正在打印出來,所以沒有問題。我怎樣纔能有ng-repeat循環排序這個userId?如果我正在處理錯誤的事情,請將我的文檔鏈接起來,以幫助我以正確的方式進行操作。如何在ng-repeat過濾器中使用範圍變量?

HTML:

User id: {{userId}} 
<br> 
User Name: 
<ul> 
    <li ng-repeat="user in users | filter:{{userId}}"> 
     {{user.name}} 
    </li> 
</ul> 

控制器:

function PhoneDetailController($scope, $routeParams, Users){ 
    $scope.userId = $routeParams.userId; 
} 
+0

我可以問一下'$ scope.users'聲明在哪裏嗎? – awmleer

+0

對不起,我離開了。它來自ngResource。有用。 – Ducksauce88

回答

2

您可以簡單地用一個$scope變量userId在NG-重複像這樣

<ul> 
    <li ng-repeat="user in users | filter:userId"> 
     {{user.name}} 
    </li> 
</ul> 

基本上只是不需要角度表達{{}}的,因爲它已經在表達式裏面。

幾個鏈接,以幫助你如果你需要。

Here is a blog explaining Two Thumb Rules決定是否使用捲髮。

When exactly to use double curly braces, single curly braces and no braces

+0

謝謝。這正是我不明白的地方。完美的作品。 – Ducksauce88

+0

爲了幫助您理解,請使用幾個良好的鏈接更新答案。 –

+0

非常感謝! – Ducksauce88

0

如果users的結構是這樣的:

[ 
    {id:1,name:'xxx'}, 
    {id:2,name:'yyy'} 
] 

您可以使用 '排序依據' 過濾器進行排序,並使用filter過濾器進行過濾:

<ul> 
    <li ng-repeat="user in users | filter:{id:userId} | orderBy: 'id' "> 
     {{user.name}} 
    </li> 
</ul> 

iduserng-repeat的財產。

這裏是單證:filterorderBy