如何過濾一個屬性的數組,取決於URL參數定義爲true或不是?我嘗試了以下,但:「錯誤:$位置未定義」。儘管如此,我已將它提供給模板的控制器。如何根據url參數過濾數組
的javascript:
app.filter('myFilter', function() {
return function(items) {
return items.filter(function(element){
var ext = ($location.search().ext == "true"); // Check if url contains ?ext=true
if (ext == true){
if (externallyAvailable == 'true') {
return true;
}
}
else{
return true;
}
});
}
});
app.controller('tilesController', ['$scope', '$http', '$sce', '$location', '$filter', function($scope, $http, $sce, $location, $filter) {
/* some controller logic */
}
模板:
<li ng-repeat="tile in list.tiles | myFilter" class="tile-wrapper">
<!-- content -->
</li>
我還發現,我的解決方案,它是否會工作,需要更多的線比我期望的,所以其他的建議都歡迎。
而且顯然沒有必要注入$位置到控制器。謝謝! – kslstn
不客氣。樂於幫助 :) –