Set defualt selected option- Angular JS












0














I am trying to add default selected option if the User role is "Supplier-Admin". Below is the code which I am working on.



When I use supplier[0] or [1] etc, it works. The company name of that element gets selected by default.



But when I use supplier[currentCompany], no option isn't getting selected as default.



$scope.supplierRole = function() {

var currentUser;
if (localStorage.getItem("currentUser") !== null) {

currentUser = JSON.parse(localStorage.getItem("currentUser"));
console.log("Received", currentUser);
} else {
console.log("Not received");
}
var currentCompany = currentUser[0].company;
if (currentUser[0].role == "Supplier-Admin") {
$scope.newUser.company = $scope.supplier[currentCompany];
return false;
} else if (currentUser[0].role == "Buyer-Admin") {
return true;
}
};


<div class="form-group" ng-hide="supplierRole()">
<label class="control-label col-sm-2">Company</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
<select class="form-control"
name="addCompany" placeholder="Select Company"
ng-options="company.name for company in supplier"
ng-model="newUser.company"
ng-required="true">
</select>

<span class="help-block"
ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
Your Company is required.
</span>
</div>
</div>











share|improve this question
























  • well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
    – Claies
    Nov 22 at 18:06












  • Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
    – Vaibhav27
    Nov 22 at 18:11












  • But still no default option is getting selected :(. The list options gets displayed. So that works well.
    – Vaibhav27
    Nov 22 at 18:13










  • it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
    – Claies
    Nov 22 at 18:14










  • also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
    – Claies
    Nov 22 at 18:16
















0














I am trying to add default selected option if the User role is "Supplier-Admin". Below is the code which I am working on.



When I use supplier[0] or [1] etc, it works. The company name of that element gets selected by default.



But when I use supplier[currentCompany], no option isn't getting selected as default.



$scope.supplierRole = function() {

var currentUser;
if (localStorage.getItem("currentUser") !== null) {

currentUser = JSON.parse(localStorage.getItem("currentUser"));
console.log("Received", currentUser);
} else {
console.log("Not received");
}
var currentCompany = currentUser[0].company;
if (currentUser[0].role == "Supplier-Admin") {
$scope.newUser.company = $scope.supplier[currentCompany];
return false;
} else if (currentUser[0].role == "Buyer-Admin") {
return true;
}
};


<div class="form-group" ng-hide="supplierRole()">
<label class="control-label col-sm-2">Company</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
<select class="form-control"
name="addCompany" placeholder="Select Company"
ng-options="company.name for company in supplier"
ng-model="newUser.company"
ng-required="true">
</select>

<span class="help-block"
ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
Your Company is required.
</span>
</div>
</div>











share|improve this question
























  • well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
    – Claies
    Nov 22 at 18:06












  • Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
    – Vaibhav27
    Nov 22 at 18:11












  • But still no default option is getting selected :(. The list options gets displayed. So that works well.
    – Vaibhav27
    Nov 22 at 18:13










  • it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
    – Claies
    Nov 22 at 18:14










  • also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
    – Claies
    Nov 22 at 18:16














0












0








0







I am trying to add default selected option if the User role is "Supplier-Admin". Below is the code which I am working on.



When I use supplier[0] or [1] etc, it works. The company name of that element gets selected by default.



But when I use supplier[currentCompany], no option isn't getting selected as default.



$scope.supplierRole = function() {

var currentUser;
if (localStorage.getItem("currentUser") !== null) {

currentUser = JSON.parse(localStorage.getItem("currentUser"));
console.log("Received", currentUser);
} else {
console.log("Not received");
}
var currentCompany = currentUser[0].company;
if (currentUser[0].role == "Supplier-Admin") {
$scope.newUser.company = $scope.supplier[currentCompany];
return false;
} else if (currentUser[0].role == "Buyer-Admin") {
return true;
}
};


<div class="form-group" ng-hide="supplierRole()">
<label class="control-label col-sm-2">Company</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
<select class="form-control"
name="addCompany" placeholder="Select Company"
ng-options="company.name for company in supplier"
ng-model="newUser.company"
ng-required="true">
</select>

<span class="help-block"
ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
Your Company is required.
</span>
</div>
</div>











share|improve this question















I am trying to add default selected option if the User role is "Supplier-Admin". Below is the code which I am working on.



When I use supplier[0] or [1] etc, it works. The company name of that element gets selected by default.



But when I use supplier[currentCompany], no option isn't getting selected as default.



$scope.supplierRole = function() {

var currentUser;
if (localStorage.getItem("currentUser") !== null) {

currentUser = JSON.parse(localStorage.getItem("currentUser"));
console.log("Received", currentUser);
} else {
console.log("Not received");
}
var currentCompany = currentUser[0].company;
if (currentUser[0].role == "Supplier-Admin") {
$scope.newUser.company = $scope.supplier[currentCompany];
return false;
} else if (currentUser[0].role == "Buyer-Admin") {
return true;
}
};


<div class="form-group" ng-hide="supplierRole()">
<label class="control-label col-sm-2">Company</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
<select class="form-control"
name="addCompany" placeholder="Select Company"
ng-options="company.name for company in supplier"
ng-model="newUser.company"
ng-required="true">
</select>

<span class="help-block"
ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
Your Company is required.
</span>
</div>
</div>








html angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 20:04









georgeawg

32.5k104967




32.5k104967










asked Nov 22 at 17:55









Vaibhav27

14




14












  • well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
    – Claies
    Nov 22 at 18:06












  • Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
    – Vaibhav27
    Nov 22 at 18:11












  • But still no default option is getting selected :(. The list options gets displayed. So that works well.
    – Vaibhav27
    Nov 22 at 18:13










  • it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
    – Claies
    Nov 22 at 18:14










  • also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
    – Claies
    Nov 22 at 18:16


















  • well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
    – Claies
    Nov 22 at 18:06












  • Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
    – Vaibhav27
    Nov 22 at 18:11












  • But still no default option is getting selected :(. The list options gets displayed. So that works well.
    – Vaibhav27
    Nov 22 at 18:13










  • it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
    – Claies
    Nov 22 at 18:14










  • also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
    – Claies
    Nov 22 at 18:16
















well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
– Claies
Nov 22 at 18:06






well, your dropdown is set to newUser.company, but your javascript sets newUser to $scope.supplier[currentUser[0].company]; does that mean the dropdown would be set to $scope.supplier[currentUser[0].company].company? seems like a typo to me.
– Claies
Nov 22 at 18:06














Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
– Vaibhav27
Nov 22 at 18:11






Oh yeah. Its supposed to be newUser.company in js file too. I updated the code now.
– Vaibhav27
Nov 22 at 18:11














But still no default option is getting selected :(. The list options gets displayed. So that works well.
– Vaibhav27
Nov 22 at 18:13




But still no default option is getting selected :(. The list options gets displayed. So that works well.
– Vaibhav27
Nov 22 at 18:13












it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
– Claies
Nov 22 at 18:14




it is a bit difficult to diagnose why your dropdown isn't selecting anything without seeing some sample data, but the first step would probably be to log out $scope.newUser.company and verify that it's actually set to a valid element from the list.
– Claies
Nov 22 at 18:14












also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
– Claies
Nov 22 at 18:16




also, is company an object or a string? That changes how ng-repeat works, and how it identifies which item is which in the list.....
– Claies
Nov 22 at 18:16

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436140%2fset-defualt-selected-option-angular-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436140%2fset-defualt-selected-option-angular-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)