2014-12-04 121 views
0

我想設置複選框默認ng檢查爲true。這是行得通的,但問題是在價值未出現之後提交。如何檢索angularjs中默認選中的複選框值?

這是我的HTML標記

<html ng-app="plunker" > 
<head> 
    <meta charset="utf-8"> 
    <title>AngularJS Plunker</title> 
    <script>document.write('<base href="' + document.location + '" />');</script> 
    <link rel="stylesheet" href="style.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script> 
    <script src="app.js"></script> 
</head> 
<body ng-controller="MainCtrl"> 
    <label><input type="checkbox" name="test1" ng-model="input.item1" ng-checked="true"/> Testing</label><br /> 
    <input type="button" ng-click="submit(input)" value="Submit" /> 
    </body> 
</html> 

腳本程序文件是

var app = angular.module('plunker', []); 
app.controller('MainCtrl', function($scope) { 
$scope.name = 'World'; 
$scope.input = {}; 
$scope.submit = function (data) { 
alert(data.item1); 
console.log($scope.input); 
}; 
}); 

plunker鏈接是http://plnkr.co/edit/mwdQF6Qqd4yw6hvLHGd6?p=preview

如何違約,並直接恢復複選框值。

謝謝。

回答

1

使用ng-init將與true初始化模式,首先設置ng-checked爲true,將無法初始化模型值

<input type="checkbox" name="test1" ng-model="input.item1" ng-init="input.item1=true" /> 

這裏是更新pulnker

+0

謝謝你的工作 – 2014-12-04 05:48:20

+0

它很樂意幫助你,如果你找到解決方案,接受答案;) – 2014-12-04 05:50:36