I need to duplicate some input fields in order to handle data from clients. I have done it with jQuery http://jsfiddle.net/m7R3f/1/
HTML:
<fieldset id="fields-list">
<div class="pure-g entry">
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="input-1" name="input-1">
</div>
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="date" name="date">
</div>
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="input-2" name="input-2">
</div>
</fieldset>
<button id="add">Add</button>
JS
$(document).ready(function ()
{
$("#add").click(function ()
{
$(".entry:first").clone(false).appendTo("#fields-list");
});
});
However I just start learning Angular and want to convert these code to Angular.
I have read questions in stackoverflow and found the code with angularjs here: http://jsfiddle.net/roychoo/ADukg/1042/. However, it seem works only for ONE input field? Can I clone/duplicate several input fields using AngularJS? (in other word: convert my code above into AngularJS version?)
Thank you very much.
If you want to clone html element, the best way to use ng-repeat directive.
Your Controller
var App = angular.module('App', []).controller('Test', ['$scope',
function($scope) {
$scope.inputCounter = 0;
$scope.inputs = [{
id: 'input'
}];
$scope.add = function() {
$scope.inputTemplate = {
id: 'input-' + $scope.inputCounter,
name: ''
};
$scope.inputCounter += 1;
$scope.inputs.push($scope.inputTemplate);
};
}
])
<!DOCTYPE html>
<html ng-app="App">
<head lang="en">
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="Test">
<fieldset id="fields-list">
<div class="pure-g entry" ng-repeat="input in inputs track by input['id']">
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="input" name="input-1">
</div>
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="date" name="date">
</div>
<div class="pure-u-1-5">
<input type="text" class="pure-input-1" id="input-2" name="input-2">
</div>
</div>
</fieldset>
<button type="button" id="add" ng-click="add()">Add</button>
</body>
</html>
Angular prevents of creation duplicated elements, to avoid this, use track by like in the example
You should create an array and use ng-repeat in your HTML. Each object in the array can contain the data necessary to populate your divs. If you want to start with three entries, then add the data for those three. If you want to add more, then simply push onto the array. Because of Angular's 2-way data binding your form field will appear once the element is pushed onto the array.
For more details on how to do this, checkout the To Do example on Angular's home page.
How about this(Fiddle)
add two more ng-model and push those models
$scope.add = function(){
$scope.items.push($scope.newitem1,$scope.newitem2,$scope.newitem3);
}
Related
I have my html elements like this:
<body ng-controller="addController">
<div class="col-sm-10">
<label class="control-label">tableName:</label>
<fieldset ng-repeat="tableName in tableNames">
<input type="text" class="form-control" ng-model="tableName.tableName" /><br />
</fieldset>
<button class="btn btn-default" ng-click="addNewTable()">Add tablename</button>
<input type="submit" ng-click="add()" />
</div>
<div>
<pre>{{tableNames|json}}</pre>
</div>
</body>
This code generates a text box every time button is clicked.
js:
function addController($scope, $http) {
$scope.tableNames = [];
$scope.addNewTable = function (tableName) {
$scope.tableNames.push({});
}
}
When i display tableNames it gives me:
[{"tableName":"textboxvalue"},{"tableName":"textboxvalue"}];
But what i intend is getting output of:
[{"tableName1":"textboxvalue","tableName2":"textboxvalue","tableName3":"textboxvalue"}];
What should I have to change in design in order to get desired output.
Thanks in advance.
You're pushing objects to the array each time but what you want is to add key value pair in the object in the array.
try this
$scope.tableNames = [{}];
$scope.addNewTable = function (tableName) {
$scope.tableNames[0].tableName = "textboxvalue" ;
}
I need to have two instances of a controller. The first instance must set a variable and the second I have to read it. The variable to set is inside the object vm (so do not use $ scope).
The code of controller is:
app.controller("AppController", function(){
var vm = this;
vm.search = null;
});
The code of first html page is:
<div class="input-group" ng-controller="AppController as app">
<input type="text" class="form-control" ng-model="app.search" placeholder="Search...">
</div>
And the code of second html page is:
<div class="input-group" ng-controller="AppController as app">
{{app.search}}
</div>
But in the second page, the value of app.search is null.
I recommend using a service to communicate data between your controller instances. This answer explains it perfectly.
But if you are keen on not using a service to share data, you can store your search variable in the $rootScope, this will make it available in all your controller instances, and in every other controller in your app. I have to warn you, this is not proper data encapsulation.
Here is how to do it:
First HTML view:
<div class="input-group" ng-controller="AppController as app">
<input type="text" class="form-control" ng-model="$root.search" placeholder="Search...">
Second HTML view:
<div class="input-group" ng-controller="AppController as app">
{{$root.search}}
</div>
I created this factory:
.factory("search", function(){
var stringaRicerca = null;
return {
getStringa: function(){
return stringaRicerca;
}, setStringa: function(stringa){
stringaRicerca = stringa;
}
};
})
And the modified controller is:
app.controller("AppController", function("search"){
var vm = this;
vm.string = search.getString;
vm.set = search.setString;
});
The first page modified is:
<div class="input-group" ng-controller="AppController as app">
<input type="text" class="form-control" ng-model="search" placeholder="Search...">
<button class="btn btn-default" ng-click="app.set(search)" type="button">GO!</button>
</div>
And the second page modified is:
<div class="input-group" ng-controller="AppController as app">
{{app.string}}
</div>
But in the second page i see anything
I have two checkboxes , one with Data Binding and other one is without Data Binding.As you can see in the code below.
<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<div>
<h2>What are your favorite sports?</h2>
<div ng-repeat="sport in ctrl.sports">
<label ng-bind="sport.label"></label>
<div>
With Binding:
<input type="checkbox" data-ng-true-value="YES" data-ng-false-value="NO" >
</div>
<div>
Using ng-checked:
<input type="checkbox" data-ng-checked='sport.selected === "YES"'>
</div>
<div>
Current state : {{sport.selected}}
</div>
</div>
</div>
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript">
angular.module('notesApp',[])
.controller('MainCtrl',[function(){
var self = this;
self.sports = [
{label:'Basketball',selected: 'YES'},
{label:'Cricket',selected:'NO'},
{label:'Soccer',selected:'NO'},
{label:'Swimming',selected:'YES'}
];
}]);
</script>
</body>
</html>
When i click on the checkbox of 'with data binding', it should bind and reflect the value in current state label.But it is not happening.Why ?.Can someone help Please .
2 problems I found with your code:
You forgot ng-model
You should specify a constant for ng-true-value and ng-false-value, by specifying YES angular will look for a YES property on the scope, write 'YES' instead
Updated working plnkr:
http://plnkr.co/edit/rnYRUpIICC7HAKdKXf3i?p=preview
<html ng-app="notesApp">
<head>
<title>Notes App</title>
</head>
<body ng-controller="MainCtrl as ctrl">
<div>
<h2>What are your favorite sports?</h2>
<div ng-repeat="sport in ctrl.sports">
<label ng-bind="sport.label"></label>
<div>
With Binding:
<input type="checkbox" ng-model="sport.selected" data-ng-true-value="'YES'" data-ng-false-value="'NO'" />
</div>
<div>
Using ng-checked:
<input type="checkbox" data-ng-checked="sport.selected === 'YES'" />
</div>
<div>
Current state : {{sport.selected}}
</div>
</div>
</div>
<script src="https://code.angularjs.org/1.4.3/angular.js"></script>
<script type="text/javascript">
angular.module('notesApp',[])
.controller('MainCtrl',[function(){
var self = this;
self.sports = [
{label:'Basketball',selected: 'YES'},
{label:'Cricket',selected:'NO'},
{label:'Soccer',selected:'NO'},
{label:'Swimming',selected:'YES'}
];
}]);
</script>
</body>
</html>
EDIT: As #g00glen00b mentioned, before angular 1.3 ng-true-value accepted only constant values, so in case you're not using angular 1.3 or later you don't have to wrap YES in quotes.
The data-ng-true-value and data-ng-false-value are used when you're using the input[checkbox] directive, but they do require ngModel, as you can see in the docs (note that ng-model is not in square brackets).
Without providing the model, AngularJS has no way to know where to apply the true/false value to. Adding data-ng-model does seem to work, as you can see in the following fiddle.
<input type="checkbox" ng-model="sport.selected"
data-ng-true-value="YES"
data-ng-false-value="NO" />
Also, please note that when using AngularJS 1.3 or higher, the values of ng-true-value and ng-false-value should be an expression rather than a constant value. Which means that for AngularJS 1.3 and higher you'll have to replace it by ng-true-value="'YES'", for example:
<input type="checkbox" ng-model="sport.selected"
data-ng-true-value="'YES'"
data-ng-false-value="'NO'" />
You are not applying "data-ng-model" attribute to the the checkbox. Please replace your "With binding" check box code from below:
<input type="checkbox" data-ng-model="sport.selected" data-ng-true-value="YES" data-ng-false-value="NO" >
I have number of check boxes in my single page
<div class="check-outer">
<label>Place of operation</label>
<div class="checkDiv">
<div ng-repeat="place in places">
<div class="checkbox-wrapper">
<label>
<input type="checkbox" ng-model="placesOBJ[place.place_id]">
<span></span>
</label>
</div>
<label class="chk-lbl">{{place.place_name}}</label>
</div>
</div>
</div>
This is working perfectly. If data is present then i want to defaultly check this check boxes
if($scope.client.client_places){
var plcLength = $scope.client.client_places.length;
var client_places = new Array();
for(var i = 0; i < plcLength; i++){
client_places[i] = $scope.client.client_places[i]['place_id'];
}
// console.log(client_places);
//$scope.placesOBJ4 = client_places;
$scope.placesOBJ = client_places;
}
client places contain an array like {1, 2}
But this is not working. if any one know about this please help me.
You can use a directive ng-checked and pass in an expression based on the model
You can use ng-checkedto achieve what you need
<input type="checkbox" ng-model="placesOBJ[place.place_id]" ng-checked="placesOBJ">
If $scope.placesOBJ is populated (not null) then the expression is true and the checkbox will be selected
Just have a look at this simple code It will be useful for you
<body ng-app="myApp" ng-controller="HomeCtrl" ng-init="init()">
<div>
<label>Place of operation</label>
<div ng-repeat="place in places" ng-show="check">
<input type="checkbox" ng-model="placesOBJ[place.id]" ng-checked="true">
<label class="chk-lbl">{{place.name}}</label>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script>
var app=angular.module('myApp', []);
app.controller('HomeCtrl', ['$scope', function($scope) {
$scope.check=false;
$scope.places=[{'id':1, 'name':'place1'},{'id':2, 'name':'place2'},{'id':3, 'name':'place3'},{'id':4, 'name':'place4'}];
$scope.init=function()
{
if($scope.places)
{
$scope.check=true;
}
}
}]);
</script>
</body>
Explanation:- You can make use of ng-init="init()" to call on page load , it will check if data is present or not. If present then set ng-show="true" else by default it will be false
I am new with AngularJs
if I want to define two model in html
how do I define it?
I haved tried it much times
please help
<body ng-app="experiment">
<div ng-controller="functionCrtl" id="staff">
<div ng-modle="data1.data">
<ng-view></ng-view>
</div>
<div ng-app="experiment_left">
<div ng-controller="accordtionCrtl">
<div ng-model="accordtion.accordtion">
<ng-view></ng-view>
</div>
</div>
experiment.config (function($routeProvider) {
$routeProvider.when("/",{
templateUrl : "function1.html",
controller: 'functionCrtl'});
I want to dynamic load two page , how can I do?
But to answer your question read this http://docs.angularjs.org/api/ng.directive:input.
Controller:
angular.module('myApp').controller('CreateUserCtrl', function ($scope) {
$scope.firstname = 'John';
$scope.surname = 'Doe';
});
HTML
<form ng-controller="CreateUserCtrl">
<input ng-model="firstname " type="text" placeholder="firstname">
<input ng-model="surname" type="text" placeholder="surname">
</form>