AngularJS ng-select issue - angularjs

I've got a simple page selector I'm using in an Angular 1.5 page. The select works for the most part, but the ng-selected part does not.
If I'm on page 3, (which is ironically the last page, not sure if that matters) 3 is selected by default. If I'm on page 1 or 2, the first item (blank) is selected. I'm not super versed in Javascript, but figured it might be a type issue. Tried settings $scope.thisPage with parseInt(page) and page.toString() just in case it did something under the sheets I wasn't aware of, but it all turned out the same.
app.controller('myCtrl', ['$scope','$route','$routeParams', function($scope,$route,$routeParams) {
$scope.pages = [];
var page = $routeParams.p ? $routeParams.p : 1
$scope.thisPage = page;
for (var i = 0; i < 5; i++) {
$scope.pages.push(i);
}
}]);
<form name="myForm">
<select ng-model="selectedPage" ng-change="switchPage(selectedPage)">
<option ng-repeat="p in pages" ng-value="{{p}}" ng-selected="{{p == thisPage}}">{{p}}</option>
</select>
</form>

You need no set selected property
<form name="myForm">
<select ng-model="selectedPage" ng-change="switchPage(selectedPage)">
<option ng-repeat="p in pages" ng-value="{{p}}">{{p}}</option>
</select>
</form>
Instead bind the selectedPage value with thisPage
app.controller('myCtrl', ['$scope','$route','$routeParams', function($scope,$route,$routeParams) {
$scope.pages = [];
var page = $routeParams.p ? $routeParams.p : 1
$scope.selectedPage= page; ////////////////////////////////
for (var i = 0; i < 5; i++) {
$scope.pages.push(i);
}
}]);

ngSelected require an expression as following p == thisPage and not {{p == thisPage}}
Source : https://docs.angularjs.org/api/ng/directive/ngSelected

Related

Connecting image from imgArray to select

I'm building a little Js project in which you can select 2 types of motorcycles, and 3 types of weather, and based on you're choices you will get street and snow tires. But right now im trying to connect an image to the option with an imgArray, but can't figure out how to do it. Right now im using onchange in html, I have to do it with eventlisterner but I didn't get that to work. Here's my code
HTML:
<select id="motor" onchange="selectBike()">
<option value="Allroad">Allroad</option>
<option value="Street">Street</option>
</select>
Js
var imgArray2 = ['KTM790', "KTM790D"];
document.querySelector("img").src = 'Bike/' + imgArray2
function selectBike() {
var x = document.getElementById('motor').value;
document.getElementById("demo").innerHTML =
"you selected " + x;
}
function selectWeather() {
var c = document.getElementById('weather').value;
document.getElementById("demo2").innerHTML = "it's " + c;
}
You need yo have an imgObjects which has the values and the images name
then you can use that code which will create image element and add it
var imgObjects = {
Allroad : "KTM790D",
Street : "KTM790"
}
function selectBike() {
var x = document.getElementById('motor').value;
document.getElementById("demo").innerHTML =
"you selected " + x;
var imgElm = document.createElement("img");
imgElm.src = window.location.hostname + "/Bikes/" + imgObjects[x]+".jpg";
console.log(imgElm)
document.getElementById("demo").appendChild(imgElm);
}
<select id="motor" onchange="selectBike()">
<option value="Allroad">Allroad</option>
<option value="Street">Street</option>
</select>
<p id="demo"></p>
In my case, You can create a JSON object and store image data into the Array object. When you adding image data, you should convert image to the base64 format. here is the link. Here is my solution
<html>
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head>
<body>
<select id="bikes" onchange="selBike()">
<option >Allroad</option>
<option >Street</option>
</select>
<div id="demo"></div>
<div id="demo1"></div>
<script>
var imgBikes = [{"imgURL" : "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGAAYAMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAACAMEBQYHAQL/xAA7EAABAwMCAwMICAYDAAAAAAABAAIDBAURBhITITEHQVEUIlRhcXSUshYXMzVikbHRI1JVcoGhFTI0/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AO4oiICIiAiIgIiICIiAiIgIiICLxzgxpc4gNAySTyAWN+kNk/rFu+KZ+6ClqvUFJpiy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59y1/u0nylRGYf4bfYEBzA4YcAR4ELo9tsVm+jVnqv+MikqKianime6m38ncLd38hhxO7uz0XOcqtHV1cTdsVVOxuMbWyuAx4Yz6h+SDZNfW+3UfksdDbY6dpfKJCKXZvc2RzMh248jtJDe4YOea1TGByVWWpqJY2xy1EsjGnLWPeSAfEAqkg2Ds+565sXvjFKdRZ7POeurF7439CpTICIiAiIgtbrC+otlXBEMySQPY0ZxklpAUe29k2sNgBoqbIHpTVI5EEcvql1f6HTfFNXv1Sav8ARKX4pqkYiCOf1Sav9Epfimp9Uer/AEWk+KH7KRiIOGaO7MtTWrVNruFbBStp6acSSFtQHHGD0GF3NEQEREH/2Q=="},{"imgURL":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////2wBDAf//////////////////////////////////////////////////////////////////////////////////////wAARCADqATkDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECA//EACQQAQEBAAIBBAMBAQEBAAAAAAABESExQQISUXFhgZGxocHw/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/xAAWEQEBAQAAAAAAAAAAAAAAAAAAEQH/2gAMAwEAAhEDEQA/AMriLyCKgg1gQwCgs4FTMOdutepjQak+FzMSVqgxZdRdPPIIvH5WzzGdBriphtTeAXg2ZjKA1pqKDUGZca3foBek8gFv8Ie3fKdA1qb8s7hoL6eLVt51FsAnql3Ut1M7AWbflLMDkEMX/F6/YjK/pADFQAUNA6alYagKk72m/j9p4Bq2fDDSYKLNXPNLoHE/NT6RYC31cJxZ3yWVM+aBYi/S2ZgiAsnYJx5D21vPmqrm3PTfpQQwyAC8JZvSKDni41ZrMuUVVl+Uz9w9v/1QWrZsZ5nFPHYH+JZyureQSF5M+fJ0CAfwRAVRBQA1DAWVUayoJUWoDpsxntPsueBV4+VxhdyAtv8AjOLGpIDMLbeGvbF4iozJfr/WukAVABAXAQXEAAASzVAZdO2WNordm+emFl7XcQSNZiFtv0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KgKoogg0lBFuIKgAAAKNRlf2gqsftsEtZWoAAqAACKoMqAAeSoqp39kL2AqLOlE8rEBFQARYALhigrNC9gGmooLp4TweEQFFBFAECgIoAu0ifIAqAAA//9k="}];
function selBike(){
var getVal = $('#bikes').val();
if(getVal == "Allroad"){
$('#demo').empty();
var imgTag = $("<img src='"+ imgBikes[0].imgURL +"'></img>");
$('#demo1').append(imgTag);
}
else{
$('#demo').empty();
var imgTag = $("<img src='"+ imgBikes[1].imgURL +"'></img>");
$('#demo').append(imgTag);
}
}
</script>
</body>
</html>
Thus, you don't need store images your directory. Because image data is now encoded.
So, Don't care about my images...
Thank you!!!
I have modified bit of your code in order to make it as you expected.
HTML:
added 'this' reference to the 'selectBike' event call to access select control in event callback function.
JS:
by default image src is map to first index of given image reference array imgArray2.
using 'this' reference which is passed as an argument we can access the selectedIndex and can change the src of the image accordingly.
var imgArray2 = ['KTM790', "KTM790D"];
//by default
document.querySelector("img").src = 'Bike/' + imgArray2[0];
function selectBike(selectRef) { var x = document.getElementById('motor').value;
document.getElementById("demo").innerHTML = "you selected " + x;
document.querySelector("img").src = 'Bike/' + imgArray2[selectRef.selectedIndex];
}
function selectWeather() { var c = document.getElementById('weather').value;
document.getElementById("demo2").innerHTML = "it's " + c;
}
<select id="motor" onchange="selectBike(this)">
<option value="Allroad">Allroad</option>
<option value="Street">Street</option>
</select>
<div id="demo"></div>
<img src="" alt="">

Unable to fill Dropdown list

I am filling 3 Dropdown lists using Angular js
Based on first dropdown selection , second dropdown will fill, and based on second dropdown third dropdown will fill.
HTML
<div ng-app ng-controller="myCtrl">
<select ng-model="option1" ng-options="option for option in options1" ng-change="getOptions2()">
</select>
<select ng-model="option2" ng-options="option for option in options2" ng-change="getOptions3()">
</select>
<select ng-model="option3" ng-options="option for option in options3">
</select>
</div>
Controller
var option1Options = ["Men", "Women", "Kids"];
var option2Options = [["Top wear","Bottom wear","Blazers"],
["W-Top Wear","W-Bottom Wear","W-Blazers"],
["K-Top wear","K-Bottom wear","K-others"]];
var option3Options = [["M-Tshirts","M-Casula Shirts","option2 - 3-3"],
["M-Jeans","option2 - 3-2","option2 - 3-3"],
["M-Blazers","option2 - 3-2","option2 - 3-3"],
["w-Tshirts","w-Casula Shirts","w-option2 - 3-3"]];
function myCtrl($scope){
$scope.options1 = option1Options;
$scope.options2 = [];
$scope.options3 = [];
$scope.getOptions2 = function(){
var key = $scope.options1.indexOf($scope.option1);
var myNewOptions = option2Options[key];
$scope.options2 = myNewOptions;
};
$scope.getOptions3 = function(){
var key = $scope.options2.indexOf($scope.option2);
var myNewOptions = option3Options[key];
$scope.options3 = myNewOptions;
};
}
Fiddle Link : http://jsfiddle.net/mayankBisht/Xku9z/513/
Problem
When I am trying to fill third dropdown with womens options, it's still showing Mens options.
Please help.

Remove select options based on button click in angular js

In my app i have a select html which has following options
"Addition","Deletion","Duplicate","Member Duplicate"
Above drop down page is common for both add and edit screen. As of now if we come from any addition click or edit click drop-down has all options. (Note: drop-down binds at the time of loading page itself. we will show/hide depending on click)
As per new requirement I need to remove all other options except "Addition" in addition click and remove "Addition" option in edit click.
select html:
<select name="ReasonID" required ng-model="member.ReasonID" class="form-control" ng-options="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description"></select>
Js
$scope.manageMember = function (member) {
$scope.showGrid = false;
$scope.showForm = true;
reset();
$scope.memberTemp = member;
angular.extend($scope.member, member); };
Please let me know if you need more details from my end.
Update :
Here the full sample code and working demo with dummy data.
HTML
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<select name="ReasonID" required ng-model="member.ReasonID" class="form-control" ng-options="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description"></select>
<br/>
<input type="button" ng-click="manageMember(undefined)" value="add"/>
<input type="button" ng-click="manageMember('bla bla bla')" value="edit"/>
</div>
</div>
JS
function TodoCtrl($scope) {
$scope.reasons = [{ID:1,Description :"Addition"}, {ID:2,Description :"Deletion"},{ID:3,Description :"Duplicate"},{ID:4,Description :"Member Duplicate"}];
var reasonsTemp =angular.copy($scope.reasons);
$scope.manageMember = function (member) {
console.log(reasonsTemp)
$scope.reasons=reasonsTemp;// assign global object to model
$scope.showGrid = false;
$scope.showForm = true;
$scope.memberTemp = member;
var EditArray=[];
for(var i = 0 ; $scope.reasons.length>i;i++)
{
if($scope.reasons[i].Description === ($scope.memberTemp == undefined ? "Addition" : "bla bla bla"))// condition for is this addition or not
{
EditArray = $scope.reasons[i];
break;
}
else // if is it not addition, then addition only offect that object. because we were already assigned original value globally
{
if($scope.reasons[i].Description!=="Addition")
{
EditArray.push($scope.reasons[i])
}
}
}
$scope.reasons=EditArray;
console.log($scope.reasons);
}
}
Working Demo On console window
Try this,
HTML
<select ng-model="selectedOption">
<option ng-show="reason.show" ng-repeat="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description">{{reason.ID}}</option>
</select>
JS
$scope.manageMember = function (member) {
$scope.showGrid = false;
$scope.showForm = true;
reset();
$scope.memberTemp = member;
angular.extend($scope.member, member);
if(member){
for(var i = 0 ; $scope.reasons.length>i;i++)
{
$scope.reasons[i].show = true;
if($scope.reasons[i].ID == "Addition"){$scope.reasons[i].show = false;}
}
}else{
for(var i = 0 ; $scope.reasons.length>i;i++)
{
$scope.reasons[i].show = false;
if($scope.reasons[i].ID == "Addition"){$scope.reasons[i].show = true;}
}
}
}
Suppose you have two buttons as,
<input type="button" ng-click="toAdd=true">Add</input>
<input type="button" ng-click="toAdd=false">Edit</input>
And the select box code should be like,
<select ng-model="selectedOption">
<option ng-show="toAdd">Addition</option>
<option ng-show="!toAdd">Deletion</option>
<option ng-show="!toAdd">Duplicate</option>
<option ng-show="!toAdd">Member Duplicate</option>
</select>
Hope this helps.

ng-repeat filter not working when range of the array is used

The array 'response' is what I get from my .php file
$http.get(encodedpage).success(function(response) {
$scope.friends = response;
}
It has a bunch of values and the filter is working correct
<input type="search" ng-model="q" />
<div class="animate-repeat" ng-repeat="x in friends | filter:q as results">
{{x.name}}
</div>
When I want to just show a range of it, the filter is not working anymore.
Although everything is displayed and shown correctly.
$http.get(encodedpage).success(function(response) {
for (var i = 0; i < 3; i++) {
$scope.friends[i] = response[i];
}
}
I am giving up why.
here is a jsfiddle example
http://jsfiddle.net/U3pVM/11646/
The problem is with the array declaration. Looks like you have declared it as an object and try to set elements into it.
$scope.friendsReduced = [];
for (var i = 0; i < 2; i++) {
$scope.friendsReduced[i] = $scope.friends[i];
}
Jsfiddle is updated for your reference.

Get/Set values of dynamical generated input fields using ng-repeat in Angular

I have an input field which once filled with a number populates that count of additional input fields. I can't figure out how to get the values inserted in those generated input fields. Any help is appreciated, and oh, I did try various things and even with angular.element, or solely with jquery, but failed, so any explanation on how to do this is welcome.
Here is my jsfiddle, and the code c/p-ed here:
<div ng-controller="MyCtrl">
<input type="text" ng-model="cntInputs" placeholder="#Inputs"><br/>
<input ng-repeat="i in getTimes()" type="text" placeholder="{{i}}" id="input_{{i}}">
<ul>
<li ng-repeat="j in getTimes()">
Inputed value is: {{getValue(j)}}
</li>
</ul>
</div>
js:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.cntInputs = 0;
$scope.getTimes=function(){
var a = new Array();
for(var i=1; i <= $scope.cntInputs; i++)
a.push(i);
return a;
};
$scope.getValue = function(id){
//here get the value of that inserted in the element with the id of "input_" + id
return angular.element("input_" + id);
}
}
So add ng-model="inputs[i-1]" to your text field
Then in your controller add $scope.inputs= [];
Then in your getValue function: return $scope.inputs[id-1];
If you make your getTimes function 0 based you wouldn't need the -1s
Updated your fiddle: http://jsfiddle.net/7MhLd/60/

Resources