2016-02-12 113 views
0

我在默认情况下检查我的角应用程序中的一些复选框。这些都是在ng-pristine state.How可以在我的控制器中接收这些输入时,表单张贴而不使他们肮脏。
<input type="checkbox" name="c1" name="favoriteColors" ng-model="data.checkboxtest[1]" ng-checked="true"> <input type="checkbox" name="c2" name="favoriteColors" ng-model="data.checkboxtest[2]">发送原始输入角控制器

控制器:

$scope.submitNewTemplate = function(){ console.log($scope.data.checkboxtest) }

返回 “未定义”

回答

0

看到我添加了一些演示代码复选框这里

请有它看看。 可能是这将帮助你

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

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.name = 'World'; 
 
    $scope.testModel = { 
 
    item1: true, 
 
    item2: false, 
 
    item3: false 
 
    }; 
 
    $scope.submit = function() { 
 
    console.log($scope.testModel); 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<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="testModel.item1" />Testing</label> 
 
    <br /> 
 
    <label> 
 
    <input type="checkbox" name="test2" ng-model="testModel.item2" />Testing 2</label> 
 
    <br /> 
 
    <label> 
 
    <input type="checkbox" name="test3" ng-model="testModel.item3" />Testing 3</label> 
 
    <br /> 
 
    <input type="button" ng-click="submit()" value="Submit" /> 
 
    <pre>{{testModel|json}}</pre> 
 
</body> 
 

 
</html>

希望这将帮助你很多。

0

好问题,我从来没有花时间看这个。 这是一个解决方法,你想要什么, ,但它做的工作。

<input type="checkbox" name="c1" name="favoriteColors" ng-model="data.checkboxtest[1]" ng-init='data.checkboxtest[1] = true'> 
<input type="checkbox" name="c2" name="favoriteColors" ng-model="data.checkboxtest[2]" ng-init='data.checkboxtest[2] = false'> 

这里就是我找到了答案How to check if any Checkbox is checked in Angular

+0

感谢,不解决我的问题answer.but。实际上,我的复选框输入将通过下拉菜单动态更改,而不会触及复选框。 –

0

您可以直接检查truefalse

if($scope.data.checkboxtest[1]){ 
    //checked 
}else{ 
    //unchecked 
}