如何查找TypeError的原因:在非对象错误上调用Object.keys

时间:2015-02-16 19:42:35

标签: angularjs

即使在其他帖子的帮助下,我似乎也能想出这个。它只发生在铬。错误发生在

  

angular.extend($ scope.selectedCompany,$ scope.companyId);

$scope.companies = {};
$scope.companies = Company.query(function () { });
$scope.selectedCompany = $sessionStorage.$default($scope.companies[1]);
$scope.selectCompany = function () {
    $rootScope.active3 = $scope.companyId;
    angular.extend($scope.selectedCompany, $scope.companyId);

    var id = $rootScope.active3
    $http.get('/api/apiCompany/' + id)
        .success(function (result) {
            $scope.CompanyName = result.CompanyName
            console.log($scope.CompanyName);
        });
};//

查看

<label>Company Name</label>
<select class="form-control" ng-model="companyId" ng-change="selectCompany()"
    ng-options="company.CompanyId as company.CompanyName for company in companies">
    {{company.CompanyName}}
</select>

这是返回json的样子

  

[{&#34; $ ID&#34;:&#34; 1&#34;&#34; CompanyId&#34;:1,&#34;公司名称&#34;:&#34; Black_Elk&# 34;,&#34;文档&#34;:空},{&#34; $ ID&#34;:&#34; 2&#34;&#34; CompanyId&#34;:2&#34;公司名称&#34;:&#34;萨拉托加&#34;&#34;文档&#34;:空},{&#34; $ ID&#34;:&#34; 3&#34;&#34; CompanyId&#34;:3,&#34;公司名称&#34;:&#34; Three_Rivers&#34;&#34;文档&#34;:空},{&#34; $ ID&#34;:&# 34; 4&#34;&#34; CompanyId&#34:4,&#34;公司名称&#34;:&#34; Transparent_Energy&#34;&#34;文档&#34;:空}]

1 个答案:

答案 0 :(得分:1)

angular.extend期望对象(请参阅here),似乎$scope.companyId只是一个数字。所以你的观点应该更像这样:

<label>Company Name</label>
  <select class="form-control" ng-model="companyId" ng-change="selectCompany()" 
    ng-options="company as company.CompanyName for company in companies">
  {{company.CompanyName}}
</select>

请注意从company.CompanyIdcompany的更改。这应该将ng-modelcompanyId(应该是company)设置为所选公司对象。有关详细信息,请参阅ng-options doc,特别注意select as label for value in array语法

的行为

希望有所帮助