2014-04-17 37 views
-1

我宣佈$ scope.something在我的控制器一樣

app.controller('MainControl', function($scope){ 
$scope.something = 1; 
}); 

話,我怎麼能訪問它在我的過濾範圍是什麼?像

app.filter("myCustomFilter", function() { 
// here 
}); 

顯然它不工作,如果我只是$ scope.something那裏。

+0

使用服務對象 – JoseM

+0

{{*** | somefilter:this}}過去工作,但我不認爲它已經做了,你不能直接傳遞一個範圍到一個過濾器,你必須做一個解決方法,像@ JoseM的解決方案 –

+0

你不能,那不是,那是什麼過濾器對於。 – Fresheyeball

回答

0

這裏聊天后是可行的解決方案http://jsbin.com/juxebewu/4/edit

<div ng-controller="sCtr"> 
    <ul> 
     <li>{{aa | friends:this}}</li></ul>{{showItem}} 
     <p ng-show="showItem">click</p> 
    </ul> 
</div> 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> 

var app = angular.module('App', []); 
var targetOfFriendTasks = 1; 
var user = []; 
user[0]= {uId: 1}; 

app.filter("friends", function() { 
    return function (input, s) { 
     var output = []; 
     for (var i in input) {    
      if (input[i].name.length >= 1 && input[i].uId == targetOfFriendTasks) {  
       output.push(input[i]); 
      } 
     } 

     if (targetOfFriendTasks != user[0].uId) { 
      return output; 
     } else { 
      s.showItem = true; 
     } 

    }; 
}); 

app.controller('sCtr', function ($scope) { 

    $scope.aa = [{name:"lorem", uId:1}, {name:"ipsum", uId:2}]; 
    $scope.showItem = false; 

}); 
+0

[僅限鏈接回答在SO](http://meta.stackexchange.com/questions/65277 /是鏈接僅-答案窮人實踐)。請將代碼複製到您的答案中。 – Chris

相關問題