在提交以调用 webapi 之前,获取 Angularjs 中变量中的输入值
Get input value in a variable in Angularjs before submit to call a webapi
我想访问可以在 AngularJS 中使用的变量中的输入字段值,以便我可以将其添加到一个字符串中,借助该字符串我可以调用 rest api。
请帮忙。
<body ng-app="myApp">
<div ng-controller="myCtr">
<form name="myForm">
<input type="text" ng-model='pinCode' id="zip" onBlur="myZipcode">
{{city}}
{{state}}
</form>
</div>
<script>
var zip;
var pat1;
function myZipcode(){
zip = document.getElementById("zip").value;
pat1 = 'http://ziptasticapi.com/'+zip;
}
var myApp = angular.module('myApp' , []);
myApp.controller('myCtr', function($scope, $http){
var path = 'http://ziptasticapi.com/12345'
$http.get(pat1).success(function (response) {
$scope.city = response.city;
$scope.state = response.state;});
});
</script>
</body>
在 http.get 服务中,如果我使用路径变量而不是 pat1,它可以工作。另一件事是,我希望州和城市动态出现,而无需提交表单并从 REST API 调用。这就是为什么我试图在变量中获取输入值来完成任务的原因
无需为pinCode
定义额外的 var,因为您使用了ng-model
因此您可以从控制器访问pinCode
。也应该使用ng-blur
而不是onBlur
。
你可以像
.HTML:
<input type="text" ng-model='pinCode' id="zip" ng-blur="myZipcode()">
控制器:
var myApp = angular.module('myApp' , []);
myApp.controller('myCtr', function($scope, $http){
$scope.pinCode= ''; // defaulr empty
var path = 'http://ziptasticapi.com/';
$scope. myZipcode = function() {
$http.get(path + $scope.pinCode).success(function (response) {
$scope.city = response.city;
$scope.state = response.state;
});
};
});
不应从控制器代码访问 html 元素。Angular 的双向数据绑定已经将表单输入的值传输到 $scope.pinCode
变量中。因此,您只需要执行一些操作即可触发服务器调用。请参阅角度文档中的此示例:https://docs.angularjs.org/api/ng/directive/ngSubmit
myApp.controller('myCtr', function($scope, $http) {
$scope.doCall = function() {
// $scope.pinCode() is set here
$scope.$http.get(...).then(
function(response) {
$scope.city = response.data.city; // or similar
}
);
}
});
只需在控制器的作用域上绑定zip
和pat1
控制器:
myApp.controller('myCtr', function($scope, $http){
$scope.zip = document.getElementById("zip").value || 0;
$scope. pat1 = 'http://ziptasticapi.com/'+ $scope.zip || '';
$scope.myZipcode();
});
然后在邮政编码中
邮政编码功能:
$scope.myZipcode = function myZipcode(){
$scope,zip = document.getElementById("zip").value;
$scop.pat1 = 'http://ziptasticapi.com/'+zip;
$http.get(pat1).success(function (response) {
$scope.city = response.city;
$scope.state = response.state;}
}
完整代码:
<body ng-app="myApp">
<div ng-controller="myCtr">
<form name="myForm">
<input type="text" ng-model='pinCode' id="zip" ng-blur="myZipcode">
{{city}}
{{state}}
</form>
</div>
<script>
myApp.controller('myCtr', function($scope, $http){
$scope.zip = document.getElementById("zip").value || 0;
$scope. pat1 = 'http://ziptasticapi.com/'+ $scope.zip || '';
$scope.myZipcode();
$scope.myZipcode = function myZipcode(){
$scope,zip = document.getElementById("zip").value;
$scop.pat1 = 'http://ziptasticapi.com/'+zip;
$http.get(pat1).success(function (response) {
$scope.city = response.city;
$scope.state = response.state;}
}
});
</script>
</body>
相关文章:
- Ionic:AngularJS变量未使用$scope更新DOM
- 浏览器's将AngularJS变量加载为URL
- 将AngularJS变量发送到新的弹出窗口
- AngularJS变量转换为html<对象>属性
- 如何在javascript数据字段中绑定AngularJs变量
- 错误:AngularJs 变量未定义,当使用 grunt 运行时.(泽泽尔)
- AngularJS变量未显示在html中
- 如何在 JavaScript 函数中包含 AngularJS 变量
- 在$http之前调用时不显示 Angularjs 变量
- 将 JavaScript 数组值分配给 AngularJS 变量
- 将 Json 文件存储在 AngularJS 变量中,以便与“ng-repeat”一起使用
- AngularJs变量未显示在视图中
- angularjs变量无法在函数调用外访问
- 如何在脚本中定义angularjs变量
- 如何从模板脚本访问AngularJS变量
- Angularjs变量undefined、null和empty同时出现
- 如何将AngularJS变量绑定为一个Polymer全局变量
- 当AngularJS变量在$scope中定义时使用对象名
- Angularjs -变量未找到
- 用JavaScript添加新的AngularJS变量