AngularJS 通過 ng-model 將表單控件與作用域模型雙向綁定,Checkbox/Radio 同理。
<input type="checkbox" ng-model="user.agreeTerms"> 同意條款
<span>狀態: {{ user.agreeTerms }}</span> <!-- true/false -->
<div ng-init="user.interests = []">
<label><input type="checkbox" value="vue" ng-model="user.interests" ng-true-value="'vue'" ng-false-value="''"> Vue</label>
<label><input type="checkbox" value="react" ng-model="user.interests" ng-true-value="'react'" ng-false-value="''"> React</label>
<label><input type="checkbox" value="angular" ng-model="user.interests" ng-true-value="'angular'" ng-false-value="''"> Angular</label>
</div>
<pre>{{ user.interests | json }}</pre> <!-- ["vue","react"] -->
關鍵點:同一 ng-model 綁定到陣列,配合 ng-true-value/ng-false-value 實現「選中添加、取消移除」。ng-false-value="''" 配合陣列去重邏輯(Angular 內部處理)。
<div>
<label><input type="radio" ng-model="user.gender" value="male"> 男</label>
<label><input type="radio" ng-model="user.gender" value="female"> 女</label>
<label><input type="radio" ng-model="user.gender" value="other"> 其他</label>
</div>
<p>選擇: {{ user.gender }}</p>
同 name 屬性的 Radio 共享一個 ng-model,選中項的 value 自動賦值。
<!-- 存儲 'yes'/'no' 而非 true/false -->
<input type="checkbox" ng-model="form.subscribe" ng-true-value="'yes'" ng-false-value="'no'">
<!-- 存儲 1/0 -->
<input type="checkbox" ng-model="form.active" ng-true-value="1" ng-false-value="0">
<!-- Controller: $scope.tags = ['前端','後端','運維','測試']; $scope.selectedTags = []; -->
<div ng-repeat="tag in tags">
<label>
<input type="checkbox"
ng-model="selectedTags"
ng-true-value="{{tag}}"
ng-false-value="''">
{{ tag }}
</label>
</div>
<button ng-click="submitTags()" ng-disabled="selectedTags.length === 0">提交選中標籤</button>
<input type="checkbox" ng-model="vm.enableFeature" ng-change="vm.onFeatureToggle()">
vm.onFeatureToggle = function() {
if (vm.enableFeature) {
Analytics.track('feature_enabled');
loadHeavyModule();
} else {
unloadModule();
}
};
<input type="checkbox" ng-model="selectAll" ng-change="toggleAll()"> 全選
<div ng-repeat="item in items">
<input type="checkbox" ng-model="item.selected"> {{ item.name }}
</div>
$scope.toggleAll = function() {
angular.forEach($scope.items, function(item) {
item.selected = $scope.selectAll;
});
};
<!-- 適合固定鍵值對,如權限矩陣 -->
<label><input type="checkbox" ng-model="permissions.read"> 讀取</label>
<label><input type="checkbox" ng-model="permissions.write"> 寫入</label>
<label><input type="checkbox" ng-model="permissions.delete"> 刪除</label>
<!-- $scope.permissions = { read: true, write: false, delete: false } -->
<form name="profileForm">
<div ng-repeat="role in availableRoles">
<label>
<input type="checkbox"
name="role_{{role.id}}"
ng-model="user.roles[role.id]"
ng-true-value="role.id"
ng-false-value="null"
required> {{ role.name }}
<span ng-show="profileForm['role_' + role.id].$error.required">至少選一個角色</span>
</label>
</div>
<button ng-disabled="profileForm.$invalid">保存</button>
</form>
每個 Checkbox 有獨立 name,納入 form.$error 統一驗證。
| 問題 | 原因 | 解決 |
|---|---|---|
| Checkbox 狀態不同步 | ng-model 綁定基本類型字符串而非對象/陣列屬性 | 確保 ng-model 指向對象屬性:ng-model="form.tags" 而非 ng-model="tags"(原始值傳遞) |
| 動態添加選項不生效 | ng-repeat 作用域隔離,ng-model 指向子作用域 | 使用對象屬性:ng-model="selection[tag]",selection 定義在父作用域 |
| Radio 未取消選中 | 同名 Radio 缺少統一 ng-model | 同組 Radio 必須共享同一 ng-model |
| ng-true-value 不生效 | 值未加引號被當作變量 | ng-true-value="'stringValue'" 單引號包裹字面量 |
[],單選用字符串/數字angular.module('formDemo', [])
.controller('FormDemoCtrl', function($scope) {
$scope.form = {
interests: [],
gender: 'male',
subscribe: 'no',
permissions: { read: true, write: false, execute: false }
};
$scope.tags = ['Vue', 'React', 'Angular', 'Svelte'];
$scope.availableRoles = [
{ id: 'admin', name: '管理員' },
{ id: 'editor', name: '編輯' },
{ id: 'viewer', name: '訪客' }
];
$scope.submit = function() {
console.log('提交數據:', angular.toJson($scope.form, true));
};
});
AngularJS 對 Checkbox/Radio 的支持極其強大:ng-model + ng-true-value/ng-false-value 組合可覆蓋 99% 的表單場景。理解「數據結構決定綁定方式」(陣列 vs 對象屬性)、「作用域繼承影響綁定目標」兩大核心原則,即可輕鬆應對複雜表單交互。配合 ng-repeat 動態渲染、ng-change 業務聯動、form.$valid 整體驗證,構建出專業級表單體驗。