[英]How to change my Angularjs $scope to accept a JavaScript variable type string
I have the following angularjs scope that: 我有以下angularjs范围:
$scope.Zips = {};
Is there a way for my angularjs scope to accept a variable that is a string? 我的angularjs范围是否可以接受字符串形式的变量? 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: 以下是JavaScript中用于检索邮政编码列表的函数:
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' || typeof miles === 'undefined' || !zip.length || !miles.length) return false;
var zips = getZips();
var zip_list = "";
if (zips.length) {
zip_list = zips.join();
zips_within_radius.value = zip_list;
}
return zip_list;
}
Image on what I am seeing: 我所看到的图像:
The issue I am having is in my form, I have the following: 我遇到的问题是我的表格,我有以下内容:
<input allow-pattern="[\d\W]" 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. 我希望
ng-model="searchParam.Zip"
检索从列表而不是从用户输入中获取的列表。
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: 我认为@FrankModica的意思是,如果您需要在范围内存储一个zip(字符串)数组,则可以执行此操作,而不是将其视为JSON对象:
$scope.Zips = [];
Have getZipCodes()
return an array and you get what it seems you're looking for. 让
getZipCodes()
返回一个数组,您将得到所寻找的东西。
If you want to keep getZipCodes()
the way you have it, then just declare the var without a type or as an empty string: 如果您要保持
getZipCodes()
,则只需声明var而不使用类型或为空字符串即可:
$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. 您可能需要重新考虑将值作为单个字符串传递到
$scope
中,但是如果确实适合您的用例,则可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.