2015-04-02 26 views
1

我收到此错误:“类型错误 - 无法设置属性选择'未定义的”在存储库函数的承诺。将对象数组添加到角度作用域对象 - 类型错误

变量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不喜欢它。

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

这里是我的控制器中的相关内容:

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) 
 
     }); 
 
}) 
 

 

,这里是我的一些库的

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') 
 
    } 
 
    } 
 
});

...现在...模块

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

+0

你能添加一个更大的代码片段吗? '$ scope'被注入了吗? – joemfb 2015-04-02 16:54:50

+0

听起来像'$ scope'是未定义的。告诉我们你的控制器的其余部分 – Tom 2015-04-02 16:59:52

+0

你能否请添加console.log(obj)? obj中有些问题。 – Samir 2015-04-02 17:10:36

回答

0

我来到那个角无法弄清楚如何处理对象数组的结论,所以我试图创建对象作为一个常规的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" 
 
    } 
 
]

0

我看到modelData和modalData在你的榜样。 我假设你写了水木清华像

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

但这是不可能没有的断码的完整版说。

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