Javascript 数组中对象的Ng重复不';行不通

Javascript 数组中对象的Ng重复不';行不通,javascript,arrays,angularjs,angularjs-ng-repeat,Javascript,Arrays,Angularjs,Angularjs Ng Repeat,我有一个从服务器检索到的对象数组。查询可以工作,但是当我在html视图中执行ng repeat时,它不工作,也不显示任何内容。为什么? 这是js代码: $scope.companyList = []; $scope.getCompanyList = function() { $scope.companyList.length = 0; CompanyListSrv.getCompanyListDetail(function (companyListDetail) {

我有一个从服务器检索到的对象数组。查询可以工作,但是当我在html视图中执行
ng repeat
时,它不工作,也不显示任何内容。为什么? 这是js代码:

$scope.companyList = [];

$scope.getCompanyList = function() {
    $scope.companyList.length = 0;

    CompanyListSrv.getCompanyListDetail(function (companyListDetail) {
        if (companyListDetail) {
            $scope.companyList = companyListDetail;
        }
    });
};

$scope.getCompanyList();
HTML代码:

   <tr ng-repeat="company in companyList">
     <td>{{ company.name }}</td>
     <td>{{ company.email }}</td> 
   </tr>
这是
0:Object

email: "text@text.com"
name: "Compant 2"
在控制台中我没有错误,在浏览器的html页面中我有以下内容:

<!-- ngRepeat: company in companyList -->

这里的问题是,angular$watch机制检查对象是否已更改,但只记得其第一次引用

console.log()
之所以有效,是因为您为该函数提供了对象的新引用

您可以执行以下操作:

if (companyListDetail) {
     for (var i = 0; i< companyListDetail; i++){
         $scope.companyList.push(companyListDetail[i]);
     }
} 
if(公司列表详细信息){
对于(var i=0;i
这里的问题是,angular$watch机制检查对象是否已更改,但只记得其第一次引用

console.log()
之所以有效,是因为您为该函数提供了对象的新引用

您可以执行以下操作:

if (companyListDetail) {
     for (var i = 0; i< companyListDetail; i++){
         $scope.companyList.push(companyListDetail[i]);
     }
} 
if(公司列表详细信息){
对于(var i=0;i
试试这个,它会起作用的:

<div ng-app ng-controller="LoginController">
<table>
<tr ng-repeat="company in companyList">
     <td>{{ company.name }}</td>
     <td>{{ company.email }}</td> 
   </tr>
</table>

</div>
function LoginController($scope) {
  $scope.companyList = [];

$scope.getCompanyList = function() {
    $scope.companyList.length = 0;
    var companyListDetail = [{
  email: "sidhantc@google.com",
  name: "Sidhant"
  },
  {
  email: "sid@google.com",
  name: "Chopper"
  }]

            $scope.companyList = companyListDetail;
            console.log($scope.companyList);
};

$scope.getCompanyList();
}
您忘记在
Html
中添加
标记

Html:

<div ng-app ng-controller="LoginController">
<table>
<tr ng-repeat="company in companyList">
     <td>{{ company.name }}</td>
     <td>{{ company.email }}</td> 
   </tr>
</table>

</div>
function LoginController($scope) {
  $scope.companyList = [];

$scope.getCompanyList = function() {
    $scope.companyList.length = 0;
    var companyListDetail = [{
  email: "sidhantc@google.com",
  name: "Sidhant"
  },
  {
  email: "sid@google.com",
  name: "Chopper"
  }]

            $scope.companyList = companyListDetail;
            console.log($scope.companyList);
};

$scope.getCompanyList();
}

正在工作的演示:

试试这个它会工作的:

<div ng-app ng-controller="LoginController">
<table>
<tr ng-repeat="company in companyList">
     <td>{{ company.name }}</td>
     <td>{{ company.email }}</td> 
   </tr>
</table>

</div>
function LoginController($scope) {
  $scope.companyList = [];

$scope.getCompanyList = function() {
    $scope.companyList.length = 0;
    var companyListDetail = [{
  email: "sidhantc@google.com",
  name: "Sidhant"
  },
  {
  email: "sid@google.com",
  name: "Chopper"
  }]

            $scope.companyList = companyListDetail;
            console.log($scope.companyList);
};

$scope.getCompanyList();
}
您忘记在
Html
中添加
标记

Html:

<div ng-app ng-controller="LoginController">
<table>
<tr ng-repeat="company in companyList">
     <td>{{ company.name }}</td>
     <td>{{ company.email }}</td> 
   </tr>
</table>

</div>
function LoginController($scope) {
  $scope.companyList = [];

$scope.getCompanyList = function() {
    $scope.companyList.length = 0;
    var companyListDetail = [{
  email: "sidhantc@google.com",
  name: "Sidhant"
  },
  {
  email: "sid@google.com",
  name: "Chopper"
  }]

            $scope.companyList = companyListDetail;
            console.log($scope.companyList);
};

$scope.getCompanyList();
}

正在工作的演示:

看不到明显的错误。尝试下一步检查:1。如何使用异步调用?使用$http?尝试放置$timeout(函数(){$scope.companyList=companyList-detail;});2.调用函数$scope.getCompanyList()后,将console.log(companyListDetail)放在函数(companyListDetail){Put console.log($scope.companyList)后面,并检查数据是否可用。问题可能是数据(companyList)无法通过
console.log($scope.companyList)
重复检查
$scope.companyList=companyList detail;
,因为它是否具有相同的对象数组。@Vitalii我可以看到此日志:[Object,Object]0:Object 1:ObjectI已解决写入“$scope.$apply()'看不到明显的错误。请尝试下一步检查:1.如何使用异步调用?使用$http?尝试在调用函数$scope.getCompanyList()后将$timeout(函数(){$scope.companyList=companyList Detail;});2.将console.log(companyList Detail)放在函数(companyList Detail){put console.log($scope.companyList)之后并检查数据是否可用。问题可能是ng repeatcheck的数据(companyList)不可用。
console.log($scope.companyList),因为它是否具有相同的对象数组。@Vitalii我可以看到此日志:[Object,Object]0:Object 1:ObjectI已经解决了编写“$scope.$apply()”的问题,它被看作是
$scope
s属性。我想简单的
$scope.$apply()
可以解决它。@vp_arth$scope.$apply()将工作,但将触发对应用程序的所有$watchs的完整检查。它会降低性能,并且如果应用程序已经在进行中,可能会引发异常。你永远不应该使用$apply!Deblaton谢谢,但它还没有工作…我想这是因为我正在进行asyn调用,因为我使用couchdban异步调用应该返回一个promise。所以我将返回两个元素,companyListDetail和promise?它被视为
$scope
s属性。我想简单的
$scope.$apply()
将修复它。@vp_arth$scope.$apply()将工作,但将触发对应用程序的所有$watchs的完整检查。它会降低性能,并且如果应用程序已经在进行中,可能会引发异常。你永远不应该使用$apply!Deblaton谢谢,但它还没有工作…我想这是因为我正在进行asyn调用,因为我使用couchdban异步调用应该返回一个承诺。所以我将返回两个元素,companyListDetail和承诺?在我正确编写的html中,我只选择了一段代码!真正的问题是在我正确编写的html中,有异步调用,我只选择了一段代码!真正的问题是有异步调用