2016-07-05 70 views
-1

我有父母和子女管理員的關係。這是基本功能的非工作模型。我敢肯定,任何比我更能勝任的人都可以在Plunker或Fiddle模型中工作。 (所以有可能是錯誤的:$ scope.data.contactList = [{ID:1,電子郵件:「[email protected]」},{ID:2,電子郵件:「[email protected]」}] ;)我試着爲contactList數組創建一些對象。Angularjs:ng-model無法正常顯示

無論如何。我希望能夠在下面的代碼中點擊第二個表中的鏈接來調用EditShowContact。在我的實際應用中,這將顯示一個隱藏的div,它顯然會顯示聯繫人的更多屬性而不僅僅是電子郵件。

在我的實際程序中,表格的值被正確填寫(即我的ng-repeat指令工作正常),但我似乎無法得到ng-model指令來響應。我嘗試了各種不同的方式,似乎沒有任何工作。

<html ng-app="myApp"><head> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> 
<script> 

var app = angular.module('myApp', []); 

app.controller('ContactsController', function ($scope) 
{ 
    this.currentContactID = null; 
    this.EditShowContact = function(intContactID) 
    { 
     this.currentContactID = intContactID; 
     //this.currentContact = $scope.data.contactList[intContactID]; unclear why this assignment fails 
    }; 
}); 

app.controller('ActionsController', function ($scope) 
{ $scope.data = {}; 
    $scope.data.contactList = [{ID: 1, Email: "[email protected]"}, {ID: 2, Email: "[email protected]"}];  
}); 

</script> 
</head> 
<body ng-controller="ActionsController as ActCtrl"> 

<div ng-controller="ContactsController as ContactsCtrl"> 
    <table border = "1";> 
     <tr><th>Email</a></th> 
      <th>Name</th></tr> 
    </table> 
    <div > 
     <table ng-repeat="Contact in ContactsCtrl.data.contactList" border="1"> 
      <tr> 
       <td><a href="" ng-click="ContactsCtrl.EditShowContact(Contact.ID)" style="padding-left: 5px;">{{Contact.Email}}</a></td> 
       <td>{{Contact.Name}}</td> 
      </tr> 
     </table> 
    </div> 
    <div> 
     <form> 
      <input type="input" ng-model="ContactsCtrl.data.contactList[currentContactID].Email"></input> 
     </form> 
    </div> 
</div> 
</body> 
</html> 

回答

1

這裏有相當多的錯誤,如:

ContactsCtrl沒有關於ContactList信息。您使用的內部<div><table>元件努力尋找使用代替ID在陣列對象中index ..

Bascially,我已減少兩個控制器的需要一個,並提出了Working Demo

+0

沒關係。我只是在你的幫助下找出了我的巨大監督。在我的完整程序中,我使用Contact.ID來引用一個不存在的contactList索引。在演示代碼中恰好遇到了ID與索引匹配的情況。這就解釋了爲什麼我的任務失敗了。 –