DOM swapping in Angular JS - angularjs

Basically I have two Divs in a page out of which only one will be visible at a time.
I have a text input placed inside my first div which is shown my default.
Now if the first div is hidden and the second DIV is shown,
I want to swap the text input DOM in to the second DIV. The ID of the textinput and the value it has needs to be retined on swapping.
I have a reference of the text input to be swapped in my custom directive (ref-comp in the below snippet).
How can i achieve this in angularJS ? Please help me here.
<div id="1">
<div id ="container1">
<input type="text" id="text1">
</input>
</div>
<div id ="container1">
<ref-comp id="ref1" refId ="text1">
</ref-comp>
</div>
</div>

Please have a look on it this might be help.
var app = angular.module('myApp', []);
app.controller('IndexCtrl', function($scope) {
$scope.showme = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="1" data-ng-app="myApp" data-ng-controller="IndexCtrl">
<div id ="container1" ng-show="showme">
value of text filed of div 1: {{divAVarialbe.text1}} <br>
<input type="text" id="text1" ng-model="divAVarialbe.text1">
</input>
</div>
<div id ="container1" ng-show="!showme">
value of text filed of div 2 : {{divBVarialbe.text1}} <br>
<input type="text" id="text1" ng-model="divBVarialbe.text1">
<ref-comp id="ref1" refId ="text1">
</ref-comp>
</div>
<button ng-click="showme = !showme" ng-show="!showme">Show div 1</button>
<button ng-click="showme = !showme" ng-show="showme">Show div 2</button>
</div>
You can use ng-show to swap the div and and scope variables to access the input filed of the DOM.
suppose that if use to scope variable lets say divAVarialbe and divBVariable.
then for div A divAVarialbe will be use i.e all the child model will be go inside divAVarialbe so your data str will be
divAVarialbe={"text1":"value of textfild","otherModel":"",...}
same for Div B.
In this way you can differentiate the DOM.
It base on how much you have knowledge of Object.
assume it like mvc pattern then it will be easy to code.
might be this will help.

Related

How can I change radio buttons to div?

I'm absolutely new to Angular.js, I got the file from someone to fix.
So the question is that I really want to change the div with clicking buttons, so I tried to search of it and i found the solution with radio button, but the thing is what i want to click is div.
So here is my code :
let vm = $scope
vm.isShown = function (color) {
return color === vm.color;
};
//this is the function in a controller
<div class="item">
<span class="group-name"
><p>PHASE 1.0</p>
Closed API Data Market</span
>
<input type="radio" ng-model="color" value="oneZero" /> 1.0
</div>
<div class="item">
<span class="group-name"
><p>PHASE 1.5</p>
Open API Data Market</span
>
<input
type="radio"
ng-model="color"
value="oneFive"
checked="checked"
/>
1.5<br />
</div>
</div>
</div>
<br />
<div class="servicePreparing" ng-show="isShown('oneZero')">
<img src="assets/img/serviceStop.jpg" />
</div>
<div class="select-list-area" ng-show="isShown('oneFive')">
I want to remove the inputs and want to click with div as class name as 'item'
You can see one example lives here: https://plnkr.co/edit/flMIhl6ugNUNXwpE
Also, what you have to do is set one variable (In the radio buttons were color), so in your AngularJS controller:
vm.color = '';
And set that value with ng-click in both divs, with the specific value, in your view you will have:
<div class="btn" ng-click="color = 'oneZero'">1.0</div>
<div class="btn" ng-click="color = 'oneFive'">1.5</div>
<div class="servicePreparing" ng-show="isShown('oneZero')">
One Zero Image Showed
</div>
<div class="select-list-area" ng-show="isShown('oneFive')">
One Five Image Showed
</div>

How to give dynamic values to ng-model

I am appending a div into the DOM on user click. To get the user entered values I want to attach ng-model to input fields. As I don't know the total number of div's which user will append, I want to keep the values in ng-model dynamic.
Here is the code: (my-Controller)
function addDiv() {
var element = angular.element(document.createElement('my-Directive'));
var el = $compile(element)($scope);
//where to place the element
angular.element($('#add')).append(element);
$scope.insertHere = el;
//giving unique id to div element.
var objName = 'step'+number;
dataCtrl.tbt = {
"val": objName,
"tes":"arjun"
}
$(element).attr("id",objName);
console.log(objName);
number++;
};
//html where I want to put dynamic ng-model
<div class="form-group">
<div class="col-xs-4">
<label for="target">Target:</label>
</div>
<div class="col-xs-7">
<input id="target" placeholder="Target" ng-model="dataCtrl.tbt[dataCtrl.tbt.val]" type="text" class="form-control"/>
</div>
You can use ng-repeat to add input boxes dynamically
<div ng-repeat="in in input">
<label>{{$index+1}}</label><input type="text" ng-model="in.value">
</div>
<button ng-click="addInput()">Add</button>
And push an empty value to the Array on ng-click of add button
Plunker Link
Hope this Helps!!

Binding in ng-repeat

I have a div which displays the text. And below it have a textarea which can edit the text of the div.On typing the text of textarea the div text changes.I don't want this to happen.The problem is both div and text area is in ng-reapeat.Otherwise I would have used different scope for both.
<div ng-repeat="item in list">
<div>
{{item.text}} // this is the text that is changing when typing in text area
</div>
<div>
<textarea rows="4" cols="50" class="form-control" ng-model="item.text" required>{{item.text}}
</textarea>
</div>
</div>
Here is the link to my issue
http://codepen.io/ankitappuria27/pen/pyVzaj
If you don't want to change the text in your div, just use a one time binding like:
{{::item.text}}
Note: Any change you make inside your textarea will ofcourse still affect your models text value. The changes just won't be visible in your div.
Example:
http://codepen.io/anon/pen/VaxGOp
Documentation:
https://docs.angularjs.org/guide/expression#one-time-binding
In your JS file you may add element in json for displaying header text just like below:
app.controller('ctrl', function($scope) {
$scope.newitem = '';
$scope.list = [
{
"order":1,
"header":"Black", // header text
"text":"Black"
},
{
"order":2,
"header":"Blue", // header text
"text":"Blue"
}
];
});
and your html for binding should be as below
<div>
{{item.header}}
</div>
<div>
<textarea rows="4" cols="50" class="form-control" ng-model="item.text" required>{{item.text}}
</textarea>
</div>

How to display of the elements one by one with angular

I want to know how to deal with angular to display an element at a time when clicking a button .
The items are hidden by default and when there is no longer of elements the button is hidden.
FIRST STEP
div one ----> always display
div two ----> hidden
div three----> hidden
button ---> click
SECOND STEP
div one ----> always display
div two ----> visible
div three----> hidden
button ---> click
THIRD STEP
div one ----> always display
div two ----> visible
div three----> visible
button ---> click-->hidden
This is my exemple : http://plnkr.co/edit/PKx3pqrSwvyzhprBN8jI
But the example the two hidden items appear on the first click
<body ng-controller="MainCtrl">
<div><input type="text" placeholder="first input"></div>
<div ng-show="state.show"><input type="text" placeholder="second input"></div>
<div ng-show="state.show"><input type="text" placeholder="third input"></div>
<button ng-click="state.show=!state.show">Add input</button>
</body>
Thank you in advance for all your answers that could help me
You can do this by maintaining a counter:
$scope.show = 0;
$scope.incrementState = function () {
$scope.show++;
}
and then in the HTML:
<div ng-show="show >= 1"><input type="text" placeholder="second input"></div>
<div ng-show="show >= 2"><input type="text" placeholder="third input"></div>
If you only need two inputs this may be best for what you need, but if you want the number of inputs to be flexible you could use ng-repeat
<body ng-controller="MainCtrl as main">
<div ng-repeat="input in main.inputs track by $index">
<input type="text" placeholder="first input">
</div>
<button ng-click="main.inputs.push('another')">Add input</button>
And in the controller: this.inputs = ["first"]

Display value of a textbox inside another textbox using AngularJs on button click

New in AngularJs. Finding it complex a little. I have created two text box and a button. Now I want, first I will write something inside the first text box. Then I will click on the button. After that the value of the first text box will get displayed inside the second text box. I have written very little code, I didn't even mentioned the button click function as I am confused on that. Please help me to go further.
<script src="../../Scripts/angular.min.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="">
<input type="text" ng-model="type">
<br />
<br />
<input type="button" value="button" onclick="myFunction()" />
<br />
<br />
<input type="text" ng-model="display" />
</div>
</body>
in your controller do ,
$scope.myFunction = function () {
$scope.display = $scope.type;
}
in your html you have to change onclick to ng-click,
<input type="button" value="button" ng-click="myFunction()" />
see this plunk for example, http://plnkr.co/edit/c5Ho1jlixwZFx7pLFm9D?p=preview
Check whether you have included angular js file in your HTML.
//add below snippet in your HTML head tag
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>

Resources