Грешка при използване на филтър за стойности от $http.get

Използвам филтър за извличане на идентификатори от повторител. Всичко работи добре, но в конзолата получавам следната грешка:

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}}" />

person peta    schedule 10.09.2014    source източник


Отговори (2)


Добре, това трябва да реши проблема ви:

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, все още не съществува.

person Josep    schedule 10.09.2014

Тъй като се връща обещание, трябва да промените $http.get('getSpecs.aspx') на return $http.get('getSpecs.aspx')

person Pramod Karandikar    schedule 10.09.2014
comment
$http.get() call е в обратно извикване на събитие, така че как може да бъде върнато? Моля обяснете. - person MiTa; 10.09.2014
comment
Все още получавам грешката с return $http.get('getSpecs.aspx') - person peta; 10.09.2014