AngularJS:范围过滤器不起作用

时间:2014-05-09 14:01:20

标签: angularjs angularjs-scope

我正在尝试保存到服务器的行程,因此我尝试同时创建2个作用域变量,但未分配的请求不会被过滤。

这是在我的控制器中:

$.when(SharePointJSOMService.getRequests())
    .done(function(jsonObject){
        $scope.requests = jsonObject.d.results;

        $scope.unassigned_requests = function(){
            return $scope.requests.filter(function(el){
            return el.Assigned_To === '';
            });
        };
    $scope.$apply();
}).fail(function(err){
    console.info(JSON.stringify(err)); 
});

我的服务包含:

// GET DIVISIONS
    this.getDivisions = function(){
        var deferred = $.Deferred();

        JSRequest.EnsureSetup();
        hostweburl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]);
        appweburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]);

        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync({
            url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('ITI_DIVISIONS')/items?$select=id,Title&@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function(data, textStatus, xhr){
                deferred.resolve(JSON.parse(data.body));
            },
            error: function(xhr, textStatus, errorThrown){
                deferred.reject(JSON.stringify(xhr));
            }
        });
        return deferred;
    }; // /getDivisions

1 个答案:

答案 0 :(得分:0)

您正在为unassigned_requests 分配功能,而不是执行实际过滤

我认为你需要这个:

function filterRequest(requests){
   return requests.filter(function(el){
         return el.Assigned_To === '';
   });
}

$.when(SharePointJSOMService.getRequests())
    .done(function(jsonObject){
        $scope.requests = jsonObject.d.results;
        $scope.unassigned_requests = filterRequest($scope.requests);
        //or  $scope.unassigned_requests = $scope.requests.filter(function(el){
        //                                      return el.Assigned_To === '';
        //                                 });
        $scope.$apply();
}).fail(function(err){
    console.info(JSON.stringify(err)); 
});