将对象数组添加到Angular范围对象 - TypeError

时间:2015-04-02 16:51:36

标签: javascript angularjs angularjs-scope

我收到此错误:" TypeError - 无法设置属性'选择'未定义"在承诺存储库功能。

变量 obj 包含一系列对象。

.then(function (obj) {
   // this works...
   var abc = {};
   abc.title = "hello";
   abc.obj = obj;

   // this doesn't work...
   $scope.modelData = {};
   $scope.title = "Import";
   $scope.message = "Please select a Purchase Request"
   $scope.select = obj; // <-- TypeError -- cannot set property 'select' of undefined'

它在角度函数期间引发错误,所以我确定它的Angular并不喜欢它。

===编辑包含更多代码。

这是我的控制器的相关位:

&#13;
&#13;
var app = ipoModule.controller("ipoController", 
                               function ($scope, $compile, $window, $http, $q, $log, ipoRepository, $routeParams, limitToFilter, $modal, ngTableParams) {

  $scope.ipoRepository = ipoRepository;

  //... not-relevant stuff

  $scope.PRList = ipoRepository.getMvpPRs($scope.POHeader.warehouseId)
            .then(function (obj) {
                var modelData = {};
                modelData.title = "MVP Import";
                modelData.message = "MVP Purchase Request ID";
                modelData.select = obj;
                $scope.modalData = modelData;

                // then do more stuff but it breaks before that...
            })
        .catch(function (err) {
            $scope.HandleErrorDoc('Search Error', err)
        });
})

  
&#13;
&#13;
&#13;

以及我的部分资料库

&#13;
&#13;
ipoModule.factory('ipoRepository', function($resource, $http) {

  var genericGet = function(params, URL) {
    return $http.get(URL, {
      params: params
    }).then(function(res) {
      if (res.data) {
        return res.data;
      } else {
        return null;
      }
    });
  };

  return {
    getSearchResults: function(txt, chk, myPO) {
      return genericGet({
        SearchText: txt,
        excludeCompleted: chk,
        MyPO: myPO
      }, '/Search/GetSearchResults');
    },

    getMvpPRs: function(id) {
      return genericGet({
        id: id
      }, '/MvpUtility/GetMvpPRs')
    }
  }
});
&#13;
&#13;
&#13;

......现在是模块......

&#13;
&#13;
var ipoModule = angular.module('ipoModule', ['ngRoute', 'ngResource', 'ngGrid', 'ui.bootstrap', 'unsavedChanges', 'ngIdle', 'ngTable'])
  .config(function($routeProvider, $locationProvider, unsavedWarningsConfigProvider) {
    //$locationProvider.html5Mode(true);
    unsavedWarningsConfigProvider.useTranslateService = false;
  });
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我得出的结论是Angular无法弄清楚如何处理一个对象数组,因此我尝试先将对象创建为常规javascript变量,然后将其分配给范围变量......并且它有效。

我会留下这个没有答案的,因为可能有一种“棱角分明”的方式来做到这一点,我不知道。

        $scope.PRList = ipoRepository.getMvpPRs($scope.POHeader.warehouseId)
            .then(function (obj) {
                var modelData = {};
                modelData.title = "MVP Import";
                modelData.message = "MVP Purchase Request ID";
                modelData.select = obj;
                $scope.modalData = modelData;

以下是我的obj变量中返回内容的示例:

[
  {
    "prid": "0000",
    "description": "PR0000 - j11-v1 test - youngm Jan 11"
  },
  {
    "prid": "0001",
    "description": "PR0001 - j11-v1 test - youngm Jan 11"
  },
  {
    "prid": "0004",
    "description": "PR0004 - j11-v1 test - j20-contractor Jan 20"
  },
  {
    "prid": "0005",
    "description": "PR0005 - tigerdirect  - yeungm Mar 01"
  }
]

答案 1 :(得分:0)

我在你的例子中看到了modelData和modalData。 我假设你写了像

的smth
$scope.modelData = {};
$scope.modalData.select = [];

但如果没有完整版本的破解代码,就无法说出来。

或者,您在第一版代码中错过了$ scope注入。 无论如何,问题与角度无关。