所以我試圖將單選按鈕綁定到對象。我花了一個小時試圖弄清楚這件事,最終承認失敗。下面是我的了:將json對象綁定到angularjs中的單選按鈕
<table>
<tr ng-repeat="theCustomer in customers">
<td>
<input type="radio" ng-model="currentCustomer" value="theCustomer" id="{{theCustomer.id}}" ng-change="currentCustomer = theCustomer">
<label for="{{theCustomer.id}}">{{theCustomer.name}}</label>
</td>
</tr>
</table>
角的東西:
bankApp.controller("BankController", function ($scope, CustomerRepository)
{
$scope.customers = [];
$scope.currentCustomer = {};
$scope.createCustomer = function() {
CustomerRepository.save($scope.customer, function (customer) {
$scope.customers.push(customer);
$scope.customer = {};
});
};
});
目前,當我嘗試點擊單選按鈕沒有反應,甚至沒有得到大關檢查。我確信有一個非常簡單的解決方案。最終目標是讓無線電選擇中反映的客戶持有currentCustomer
。
所以這個主要工作,唯一的問題是'currentCustomer'被設置爲字符串化json而不是對象。有這個簡單的解決辦法嗎?我試圖做到這一點,沒有雙大括號,並沒有工作。 – 2013-04-30 14:32:03
你希望最終結果看起來像什麼? – rGil 2013-05-01 14:59:20
其實我想通了。 value屬性只接受一個字符串,不能處理一個對象。我重構了這個。謝謝! – 2013-05-01 15:30:57