2013-12-19 39 views
-1

的JavaScript为什么推不更新列表

$scope.addCustomer = function() {  
    $scope.customers=[{name:'bob',city:'delhi'},{name:'david',city:'bombay'}]; 
    $scope.customers.push({name:$scope.newCustomer.name,city:$scope.newCustomer.city 
} 

HTML

<br/> 
Customer name :<br /> 
<input type="text" ng-model="newCustomer.name" /> 
<br/> 
Customer city :<br /> 
<input type="text" ng-model="newCustomer.city" /> 
<button ng-click="addCustomer()">Add customer</button> 

我试着列出所有客户名称在一个无序列表,但新客户没有被添加到该列表。

如何将niew客户添加到列表中?

+0

你的JS有无效的语法。 – Stewie

+0

可以请你指点一下吗? – user2516048

+0

'$ scope.customers.push({...});' – Stewie

回答

0

试试这个:

$scope.customers=[{name:'bob',city:'delhi'},{name:'david',city:'bombay'}]; 
$scope.addCustomer = function() { 
    $scope.customers.push({name:$scope.newCustomer.name,city:$scope.newCustomer.city}); 
} 

工作〔实施例:http://jsfiddle.net/bateast/aVWpQ/

+0

它适用于您提供的链接。但是当我在本地机器上运行时,没有什么会在我的浏览器中反映出来 – user2516048

0

不要在addCustomer()值分配给$scope.customers

你可以试试这个:

<div ng-controller="test"> 
    Customer name :<br /> 
    <input type="text" ng-model="newCustomer.name" /> 
    <br/> 
    Customer city :<br /> 
    <input type="text" ng-model="newCustomer.city" /> 
    <button ng-click="addCustomer()">Add customer</button> 

    <table> 
     <thead> 
      <tr> 
       <th>name</th> 
       <th>city</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="customer in customers"> 
       <td>{{customer.name}}</td> 
       <td>{{customer.city}}</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 
<script> 
    function test($scope){ 
     $scope.customers=[{name:'bob',city:'delhi'},{name:'david',city:'bombay'}]; 
     $scope.addCustomer = function(){ 
      $scope.customers.push({name:$scope.newCustomer.name,city:$scope.newCustomer.city}); 
      alert($scope.customers.length) 
     } 
    }  
</script>