How to change my Angularjs $scope to accept a JavaScript variable type string










0














I have the following angularjs scope that:



$scope.Zips = ;


Is there a way for my angularjs scope to accept a variable that is a string? I would like to retrieve my string (that is a list) in the following function:



$scope.GetCurrentZip = function ()
try
$scope.Zips = $parse(getZipCodes());
catch(err)





The following is the function in JavaScript that retrieves the list of Zip Codes:



function getZipCodes() 
var miles = document.getElementById("miles").options[document.getElementById("miles").selectedIndex].innerHTML;
var zip = document.getElementById("zip").value;
var zips_within_radius = document.getElementById("zipsWithinRadius");
// debugger;
if (typeof zip === 'undefined'


Image on what I am seeing:



enter image description here



The issue I am having is in my form, I have the following:



<input allow-pattern="[dW]" class="form-control" id="zip" maxlength="5"
ng-model="searchParam.Zip" placeholder="Zip code" type="text" />


I would like the ng-model="searchParam.Zip" to retrieve the list I am getting from the list instead from the user input.










share|improve this question



















  • 1




    You can put whatever you want on the scope
    – Frank Modica
    Nov 12 '18 at 22:17










  • @FrankModica: I understand that but I am not able to see the values that are being passed in.
    – user9808783
    Nov 12 '18 at 22:19










  • You mean see them in the HTML? If so, you should show the template.
    – Frank Modica
    Nov 12 '18 at 22:21










  • What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
    – Frank Modica
    Nov 12 '18 at 22:31










  • @FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
    – user9808783
    Nov 12 '18 at 22:37
















0














I have the following angularjs scope that:



$scope.Zips = ;


Is there a way for my angularjs scope to accept a variable that is a string? I would like to retrieve my string (that is a list) in the following function:



$scope.GetCurrentZip = function ()
try
$scope.Zips = $parse(getZipCodes());
catch(err)





The following is the function in JavaScript that retrieves the list of Zip Codes:



function getZipCodes() 
var miles = document.getElementById("miles").options[document.getElementById("miles").selectedIndex].innerHTML;
var zip = document.getElementById("zip").value;
var zips_within_radius = document.getElementById("zipsWithinRadius");
// debugger;
if (typeof zip === 'undefined'


Image on what I am seeing:



enter image description here



The issue I am having is in my form, I have the following:



<input allow-pattern="[dW]" class="form-control" id="zip" maxlength="5"
ng-model="searchParam.Zip" placeholder="Zip code" type="text" />


I would like the ng-model="searchParam.Zip" to retrieve the list I am getting from the list instead from the user input.










share|improve this question



















  • 1




    You can put whatever you want on the scope
    – Frank Modica
    Nov 12 '18 at 22:17










  • @FrankModica: I understand that but I am not able to see the values that are being passed in.
    – user9808783
    Nov 12 '18 at 22:19










  • You mean see them in the HTML? If so, you should show the template.
    – Frank Modica
    Nov 12 '18 at 22:21










  • What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
    – Frank Modica
    Nov 12 '18 at 22:31










  • @FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
    – user9808783
    Nov 12 '18 at 22:37














0












0








0







I have the following angularjs scope that:



$scope.Zips = ;


Is there a way for my angularjs scope to accept a variable that is a string? I would like to retrieve my string (that is a list) in the following function:



$scope.GetCurrentZip = function ()
try
$scope.Zips = $parse(getZipCodes());
catch(err)





The following is the function in JavaScript that retrieves the list of Zip Codes:



function getZipCodes() 
var miles = document.getElementById("miles").options[document.getElementById("miles").selectedIndex].innerHTML;
var zip = document.getElementById("zip").value;
var zips_within_radius = document.getElementById("zipsWithinRadius");
// debugger;
if (typeof zip === 'undefined'


Image on what I am seeing:



enter image description here



The issue I am having is in my form, I have the following:



<input allow-pattern="[dW]" class="form-control" id="zip" maxlength="5"
ng-model="searchParam.Zip" placeholder="Zip code" type="text" />


I would like the ng-model="searchParam.Zip" to retrieve the list I am getting from the list instead from the user input.










share|improve this question















I have the following angularjs scope that:



$scope.Zips = ;


Is there a way for my angularjs scope to accept a variable that is a string? I would like to retrieve my string (that is a list) in the following function:



$scope.GetCurrentZip = function ()
try
$scope.Zips = $parse(getZipCodes());
catch(err)





The following is the function in JavaScript that retrieves the list of Zip Codes:



function getZipCodes() 
var miles = document.getElementById("miles").options[document.getElementById("miles").selectedIndex].innerHTML;
var zip = document.getElementById("zip").value;
var zips_within_radius = document.getElementById("zipsWithinRadius");
// debugger;
if (typeof zip === 'undefined'


Image on what I am seeing:



enter image description here



The issue I am having is in my form, I have the following:



<input allow-pattern="[dW]" class="form-control" id="zip" maxlength="5"
ng-model="searchParam.Zip" placeholder="Zip code" type="text" />


I would like the ng-model="searchParam.Zip" to retrieve the list I am getting from the list instead from the user input.







javascript angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:21









georgeawg

32.7k104967




32.7k104967










asked Nov 12 '18 at 22:14









user9808783

5910




5910







  • 1




    You can put whatever you want on the scope
    – Frank Modica
    Nov 12 '18 at 22:17










  • @FrankModica: I understand that but I am not able to see the values that are being passed in.
    – user9808783
    Nov 12 '18 at 22:19










  • You mean see them in the HTML? If so, you should show the template.
    – Frank Modica
    Nov 12 '18 at 22:21










  • What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
    – Frank Modica
    Nov 12 '18 at 22:31










  • @FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
    – user9808783
    Nov 12 '18 at 22:37













  • 1




    You can put whatever you want on the scope
    – Frank Modica
    Nov 12 '18 at 22:17










  • @FrankModica: I understand that but I am not able to see the values that are being passed in.
    – user9808783
    Nov 12 '18 at 22:19










  • You mean see them in the HTML? If so, you should show the template.
    – Frank Modica
    Nov 12 '18 at 22:21










  • What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
    – Frank Modica
    Nov 12 '18 at 22:31










  • @FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
    – user9808783
    Nov 12 '18 at 22:37








1




1




You can put whatever you want on the scope
– Frank Modica
Nov 12 '18 at 22:17




You can put whatever you want on the scope
– Frank Modica
Nov 12 '18 at 22:17












@FrankModica: I understand that but I am not able to see the values that are being passed in.
– user9808783
Nov 12 '18 at 22:19




@FrankModica: I understand that but I am not able to see the values that are being passed in.
– user9808783
Nov 12 '18 at 22:19












You mean see them in the HTML? If so, you should show the template.
– Frank Modica
Nov 12 '18 at 22:21




You mean see them in the HTML? If so, you should show the template.
– Frank Modica
Nov 12 '18 at 22:21












What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
– Frank Modica
Nov 12 '18 at 22:31




What do you get if you do console.log(getZipCodes()) or console.log ($parse(getZipCodes()))?
– Frank Modica
Nov 12 '18 at 22:31












@FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
– user9808783
Nov 12 '18 at 22:37





@FrankModica: I am not getting anything. I would like to see if the list is being passed through. Currently, I only see the zip code that was entered
– user9808783
Nov 12 '18 at 22:37













1 Answer
1






active

oldest

votes


















0














I think what @FrankModica is saying is, if you need to store an array of zips (strings) in your scope, you can do that rather than thinking of it as a JSON object:



$scope.Zips = ;


Have getZipCodes() return an array and you get what it seems you're looking for.



If you want to keep getZipCodes() the way you have it, then just declare the var without a type or as an empty string:



$scope.Zips;
$scope.Zips = '';


You may want to reconsider passing the value into the $scope as a single string, but if that really works for your use case there you go.






share|improve this answer






















  • @ jgraff: So there isn't a way for the scope to accept a string that is a list?
    – user9808783
    Nov 12 '18 at 22:29










  • Absolutely - see edit.
    – jgraff
    Nov 12 '18 at 22:43










  • Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
    – user9808783
    Nov 12 '18 at 23:07










  • Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
    – user9808783
    Nov 12 '18 at 23:10











  • I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
    – jgraff
    Nov 14 '18 at 1:23











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%2f53270873%2fhow-to-change-my-angularjs-scope-to-accept-a-javascript-variable-type-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think what @FrankModica is saying is, if you need to store an array of zips (strings) in your scope, you can do that rather than thinking of it as a JSON object:



$scope.Zips = ;


Have getZipCodes() return an array and you get what it seems you're looking for.



If you want to keep getZipCodes() the way you have it, then just declare the var without a type or as an empty string:



$scope.Zips;
$scope.Zips = '';


You may want to reconsider passing the value into the $scope as a single string, but if that really works for your use case there you go.






share|improve this answer






















  • @ jgraff: So there isn't a way for the scope to accept a string that is a list?
    – user9808783
    Nov 12 '18 at 22:29










  • Absolutely - see edit.
    – jgraff
    Nov 12 '18 at 22:43










  • Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
    – user9808783
    Nov 12 '18 at 23:07










  • Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
    – user9808783
    Nov 12 '18 at 23:10











  • I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
    – jgraff
    Nov 14 '18 at 1:23
















0














I think what @FrankModica is saying is, if you need to store an array of zips (strings) in your scope, you can do that rather than thinking of it as a JSON object:



$scope.Zips = ;


Have getZipCodes() return an array and you get what it seems you're looking for.



If you want to keep getZipCodes() the way you have it, then just declare the var without a type or as an empty string:



$scope.Zips;
$scope.Zips = '';


You may want to reconsider passing the value into the $scope as a single string, but if that really works for your use case there you go.






share|improve this answer






















  • @ jgraff: So there isn't a way for the scope to accept a string that is a list?
    – user9808783
    Nov 12 '18 at 22:29










  • Absolutely - see edit.
    – jgraff
    Nov 12 '18 at 22:43










  • Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
    – user9808783
    Nov 12 '18 at 23:07










  • Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
    – user9808783
    Nov 12 '18 at 23:10











  • I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
    – jgraff
    Nov 14 '18 at 1:23














0












0








0






I think what @FrankModica is saying is, if you need to store an array of zips (strings) in your scope, you can do that rather than thinking of it as a JSON object:



$scope.Zips = ;


Have getZipCodes() return an array and you get what it seems you're looking for.



If you want to keep getZipCodes() the way you have it, then just declare the var without a type or as an empty string:



$scope.Zips;
$scope.Zips = '';


You may want to reconsider passing the value into the $scope as a single string, but if that really works for your use case there you go.






share|improve this answer














I think what @FrankModica is saying is, if you need to store an array of zips (strings) in your scope, you can do that rather than thinking of it as a JSON object:



$scope.Zips = ;


Have getZipCodes() return an array and you get what it seems you're looking for.



If you want to keep getZipCodes() the way you have it, then just declare the var without a type or as an empty string:



$scope.Zips;
$scope.Zips = '';


You may want to reconsider passing the value into the $scope as a single string, but if that really works for your use case there you go.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 22:42

























answered Nov 12 '18 at 22:25









jgraff

215




215











  • @ jgraff: So there isn't a way for the scope to accept a string that is a list?
    – user9808783
    Nov 12 '18 at 22:29










  • Absolutely - see edit.
    – jgraff
    Nov 12 '18 at 22:43










  • Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
    – user9808783
    Nov 12 '18 at 23:07










  • Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
    – user9808783
    Nov 12 '18 at 23:10











  • I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
    – jgraff
    Nov 14 '18 at 1:23

















  • @ jgraff: So there isn't a way for the scope to accept a string that is a list?
    – user9808783
    Nov 12 '18 at 22:29










  • Absolutely - see edit.
    – jgraff
    Nov 12 '18 at 22:43










  • Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
    – user9808783
    Nov 12 '18 at 23:07










  • Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
    – user9808783
    Nov 12 '18 at 23:10











  • I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
    – jgraff
    Nov 14 '18 at 1:23
















@ jgraff: So there isn't a way for the scope to accept a string that is a list?
– user9808783
Nov 12 '18 at 22:29




@ jgraff: So there isn't a way for the scope to accept a string that is a list?
– user9808783
Nov 12 '18 at 22:29












Absolutely - see edit.
– jgraff
Nov 12 '18 at 22:43




Absolutely - see edit.
– jgraff
Nov 12 '18 at 22:43












Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
– user9808783
Nov 12 '18 at 23:07




Would i need to update the following code: if(searchParam) //debugger; $scope.formModel = searchParam; for(var param in searchParam) if(searchParam.hasOwnProperty(param)) var paramValue = searchParam[param].value ? searchParam[param].value.trim() : searchParam[param].trim(); if (paramValue.length) queryString += param + '=' + paramValue + '&';
– user9808783
Nov 12 '18 at 23:07












Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
– user9808783
Nov 12 '18 at 23:10





Better yet. I need the following $scope.ZipsWithinRadius to be included in the searchParam query string. How can I do that? That is the problem I am seeing. It isnt passing the value over
– user9808783
Nov 12 '18 at 23:10













I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
– jgraff
Nov 14 '18 at 1:23





I think it's time to back up and decide what pattern you want to follow. Passing big string values by query string is risky and hard to manage. Typically you would call a service to retrieve data for your model. I would encourage you to look up some examples of getting data from services before going further down this route.
– jgraff
Nov 14 '18 at 1:23


















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%2f53270873%2fhow-to-change-my-angularjs-scope-to-accept-a-javascript-variable-type-string%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党