0

I am trying to set checkbox default ng-checked true. This is working but the problem is submitting after value not appearing.

This is my html markup

<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>

Scripting app file is

 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 link is http://plnkr.co/edit/mwdQF6Qqd4yw6hvLHGd6?p=preview

How to default and directly reviving checkbox value.

Thank you.

1 Answer 1

1

use ng-init that will initialize the model with true, setting ng-checked to true at first will not initialize the model value

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

here is the updated pulnker

Sign up to request clarification or add additional context in comments.

1 Comment

its glad to help you, accept the answer if u found the solution ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.