2014-12-04 135 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