2014-09-10 61 views
1

我正在使用过滤器从中继器中提取id。这一切运作良好,但在控制台中我收到以下错误:

https://docs.angularjs.org/error/$interpolate/interr?p0=%7B%7BspecsList%20%7C%20splitIds%7D%7D&p1=TypeError:%20Cannot%20read%20property%20%27map%27%20of%20undefined 

如果我更换$http.get静态值specsList,没有错误。任何想法如何以良好的方式解决它?

JS:

app.controller('SpecsController', function ($scope, $http) { 
    $scope.$on('$viewContentLoaded', function(event) { 
     $http.get('getSpecs.aspx') 
      .then(function(result) { 
      $scope.specsList = result.data.specs; 
      }) 
      .catch(function(err) { 
      console.log(err); 
      }); 
    }); 

    $scope.sortableOptions = { 
     handle: "> .position", 
     helper: function (e, ui) { 
       var originals = ui.children(); 
       var clonedHelper = ui.clone(); 
       clonedHelper.children().each(function(index) { 
       //$(this).width($(this).width()); 
       $(this).width(originals.eq(index).outerWidth()); 
       $(this).addClass("dragging"); 
       $(this).parent().addClass("dragging_tr"); 
       }); 
       return clonedHelper; 
     }, 
     update: function(e, ui) { 
      //console.log("update"); 
     }, 
     stop: function(e, ui) { 
      //Save sortorder 
     } 
    }; 
}); 

app.filter('splitIds', function() { 
    return function(ArrayWithIds) { 
     return ArrayWithIds.map(function(item){return item.id;}).join(); 
    }; 
}); 

HTML:

<input type="text" value="{{specsList | splitIds}}" /> 

回答

0

确定,这应该解决您的问题:

app.filter('splitIds', function() { 
    return function(ArrayWithIds) {   
     return (angular.isUndefined(ArrayWithIds) || !angular.isArray(ArrayWithIds))? 
       "": 
       ArrayWithIds.map(function(item){return item.id;}).join(); 
    }; 
}); 

发生了什么事是,第一次过滤器splitIds被称为$scope.specsList尚不存在。

+0

谢谢!真的很感激它! – peta 2014-09-10 17:57:59

0

自从被返回的承诺是,你需要改变$http.get('getSpecs.aspx')return $http.get('getSpecs.aspx')

+0

'$ http.get()'调用是在事件回调中,所以如何返回?请解释。 – MiTa 2014-09-10 08:52:47

+0

我仍然收到错误返回$ http.get('getSpecs.aspx') – peta 2014-09-10 12:16:37

相关问题