I have tried that, if i check the check box then radio button will also get selected. Its going good when i check the check box.
But, If i select the radio button binding process is not happening. i.e, If i click the radio button ,the check box has to be checked.
I dont know how to do this?
Herewith placed the code.
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-model="checked">
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above:
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked">Yeah :)
</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= 0;
}]);
</script>
</body>
</html>
Can anyone please help on this?
Try this
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
}]);
</script>
<body ng-app="sampleApp">
<div ng-controller="sampleController" ng-init="checked='false'">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-click="checked = !checked">
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above:
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked" ng-click="checked = !checked">Yeah :)
</div>
You could do something like this,it will definitely work
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="slave" ng-model="checked">
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above:
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked" ng-click="slave=true">Yeah :)
</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= false;
$scope.slave=false;
}]);
</script>
</body>
</html>
I have introduced another variable which will change on click of the radio button,because the event which gets triggered from the radio button is not the check event rather it is the onclick event.
Try this, Its worked fine..!
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Learn It HTML Template</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="sampleController">
Are you going for party tonight: <input type="checkbox" ng-checked="checked" ng-click="toggleSwitch()">
<br/> <br/>
You should go, Complete this example and rest examples you can learn tomorrow :), So click on the check box above:
<br/> <br/>
<input id="checkSlave" type="radio" ng-checked="checked" ng-click="toggleSwitch()">Yeah :)
</div>
<script>
var app = angular.module("sampleApp", []);
app.controller('sampleController', ['$scope', function ($scope) {
$scope.checked= false;
$scope.toggleSwitch=function(){
$scope.checked= !$scope.checked;
}
}]);
</script>
</body>
</html>
</html>
Here you did not specify the two way binding directive i.e ng-model for radio button.
<input id="checkSlave" type="radio" ng-checked="checked" ng-model="checked">Yeah :)
Related
I am new to angularjs. I am trying a simple program which have check boxes and a disabled button. The button should be enabled after one or multiple checkboxes are selected. However, my solution seems to be not working. Any help in this respect will be great.
HTML code:
<html ng-app="Apps" ng-controller = "chk">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript" src="Apps.js"></script>
</head>
<body>
<center>
<h1> Fruits </h1>
<hr/>
<div >
<label ng-repeat="lbl in lbls">
<input type="checkbox" ng-model="chkd" > {{lbl}}
</label>
<br>
<input type="button" value="Submit" ng-disabled="!chkd"/>
</div>
</center>
</body>
</html>
here is the JS is file:
var Apps = angular.module ('Apps', [])
Apps.controller("chk", function($scope){
$scope.lbls = [
'Apples',
'Bananas',
'Apricots',
'Peaches'
];
});
It looks like you have to have separate conditions for each of the checkboxes for ng-disabled to detect a change. Use this HTML layout instead:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">
</script>
</head>
<body>
<center>
<h1> Fruits </h1>
<hr/>
<div ng-app>
<input type="checkbox" ng-model="cb01"> Apples
<input type="checkbox" ng-model="cb02"> Bananas
<input type="checkbox" ng-model="cb03"> Apricots
<input type="checkbox" ng-model="cb04"> Peaches
<br>
<input type="button" value="Submit" ng-disabled="!(cb01||cb02||cb03||cb04)">
</div>
</center>
</body>
</html>
jsfiddle
EDIT
Code should now be working fine. Sorry for the confusion!
Here is an approach which you can insert other fruits, and don't need to change code in your ng-disabled:
var Apps = angular.module ('Apps', [])
Apps.controller("chk", function($scope){
$scope.chkd = {
Apples: false,
Bananas: false,
Apricots: false,
Peaches: false
};
$scope.enableSubmit = function(){
var atLeastOneSelected = [];
for (var fruit in $scope.chkd) {
if($scope.chkd[fruit] === true){
atLeastOneSelected.push(fruit);
}
}
return !(atLeastOneSelected.length > 0);
}
});
<html ng-app="Apps" ng-controller = "chk">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript" src="Apps.js"></script>
</head>
<body>
<center>
<h1> Fruits </h1>
<hr/>
<div >
<label ng-repeat="(lbl, enabled) in chkd">
<input type="checkbox" ng-model="chkd[lbl]"> {{lbl}}
</label>
<br>
<input type="button" value="Submit" ng-disabled="enableSubmit()"/>
</div>
</center>
</body>
</html>
This is a problem that can be easily done by declaring a scope function but I was wondering if there is a better way to make this easier
The code is:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body ng-controller="myCtrl">
<input type="checkbox" ng-model="activate" id="activate"> ACTIVATE <br />
<input type="text" ng-model="first" ng-disabled="!activate">
<input type="text" ng-model="second" ng-disabled="!activate">
<!-- <span id="clickMe">Click Me!</span> -->
<script type="text/javascript">
app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout){
});
</script>
</body>
</html>
So this is the case. The other two fields will be enabled if the checkbox is checked and true and will be disabled if the checkbox is unchecked. What I want is, to make the fields go null or blank automatically upon disabling.
Here is an example: http://plnkr.co/edit/2EPuhRiR9OncV8z7q018?p=preview
Ignoring the fact that it is not a good practice to put too much logic directly in the view, a technical solution would be:
<input type="checkbox" ng-model="activate" id="activate" ng-change="sameAsAbove(first, second); value = null"> ACTIVATE <br />
<input type="text" ng-model="value.first" ng-disabled="!activate">
<input type="text" ng-model="value.second" ng-disabled="!activate">
I try to make disable second field until I do not enter 'Lokesh' in first field for that I have tried below code I am in learning stage of angularjs so please correct me where I am doing mistake.
eg.code
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<p>Enter 'Lokesh' to display last name</p>
<div ng-app="myApp" ng-controller="myCtrl">
<input data-ng-model="firstName"
type="text"
ng-disabled="{if(firstName="lokesh")}"
Last Name: <input type="text" ng-model="lastName"><br>
/>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lastName= "Kumar Gaurav";
});
</script>
</body>
The problem is with the attribute ng-disabled="{if(firstName=" lokesh ")}"
There is no need of using if, you can directly compare the strings.
Update the code as
ng-disabled="firstName=='lokesh'"
Demo
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app>
<input data-ng-model="firstName" class="span12 editEmail" type="text" placeholder="Type lokesh here" />Last Name:
<input type="text" ng-model="lastName" ng-disabled="firstName!=='lokesh'" />
<br>{{firstName}}
</div>
I am new in angularjs. What I am trying to do is adding a simple controller in an angular app. So, my code is like this-
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div ng-app="">
<div ng-init="mySwitch=true">
<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>
<p>
<input type="checkbox" ng-model="mySwitch"/>Button Disable
</p>
<p>
{{ mySwitch }}
</p>
</div>
</div>
</body>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>
So, it is working perfectly-
When I addd ng-controller="anything" as such...
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div ng-app="">
<div ng-controller="anything" ng-init="mySwitch=true">
<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>
<p>
<input type="checkbox" ng-model="mySwitch"/>Button Disable
</p>
<p>
{{ mySwitch }}
</p>
</div>
</div>
</body>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>
Nothing seems to work.
Can anyone please help, what I am doing wrong?
Thanks in advance for helping.
Looks like you have not defined a controller. Observe the following...
<div ng-app="app">
<div ng-controller="ctrl">
[...]
angular.module('app', []).controller('ctrl', function($scope) {
$scope.mySwitch = true;
});
JSFiddle Link - working demo
And as always, refer to the Understanding Controllers docs for more information.
in the first step you need to declare a name for your app
for exemple
Module.js
var home = angular.module("home",['ngRoute']);
after that call this var in your controller
Controller.js
home.controller('homeController',function($scope){
$scope.switch = value;
});
in your code html
index.html
<!DOCTYPE html>
<html ng-app="home">
<div ng-controller="homeController" >
{{switch}}
</div>
Here is a simple code, where I tried to implement what I'm talking about:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example32-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.0/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="AppController">
<form name='AlertForm' ng-init="action = 'AlertForm.$valid && print(text)'">
Input: <input type="text" ng-model="text" ng-keyup='$event.keyCode == 13 && {{action}}' /><br />
<input type="button" ng-click="{{ action }}" value="Alert" />
</form>
</div>
<script>
angular.module('app', [])
.controller('AppController', ['$scope', function($scope) {
$scope.print = function(msg) {
alert(msg);
};
}]);
</script>
</body>
</html>
http://plnkr.co/edit/M7hfHytCzqrOMMLSpbZm?p=preview
There is a form with a single input and a button. If you press the button or press enter, while staying in the input, it will alert you with content of the input.
I've tried to use ng-init and Angular templating for caching the print(text) expression, to avoid redundancy, but it doesn't work.
Is there any other ways, to do that without touching controller?