在我的HTML文件中调用函数后,我不得不重新获取值。我做了一些研究,尝试了一些事但没有成功。请参阅以下代码:
HTML:
<div class="form-group">
<div class="col-sm-9"><label for="IPAddr">IP Address</label><input ng-model="IPAddr" id="IPAddr" name="IPAddr" ng-blur="IPAvail=validateIP()"
type="text" class="form-control">{{IPAvail}}</div>
</div>
带有IP地址输入字段的基本表单。调用validateIP(),并显示返回值“{{IPAvail}}”。
JS:
$scope.validateIP = function(IPAvail) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
var ipVar = $.param({ip: $scope.ipadd});
$http({
url: 'https://test.com/flask_server/findIP',
method: "POST",
data: ipVar
})
.then(function(response) {
$scope.result = response.data;
ip = response.data;
if (ip.indexOf("10.10.10.1") >=0) {
alert("Matches with IP")
IPAvail = "Yes"
return IPAvail
} else {
alert("Does not match with IP")
}
IPAvail = "No"
return IPAvail
});
};
我简化了代码以使其易于阅读但基本上,一切都正常工作但我遇到的唯一问题是将IPAvail变量返回到HTML,因此可以在这里使用:
type="text" class="form-control">{{IPAvail}}</div>
由于 戴蒙
答案 0 :(得分:1)
不是返回IPAvail
,而是将$scope.IPAvail
设置为所需的值。您应该遵循onblur
。
ng-change="validateIP()"
使用ng-change
允许AngularJS了解效果。否则,您可能会遇到AngularJS 而不是知道的问题。 AngularJS最好知道。