Javascript ng submit调用工厂函数后如何更新视图?
在ng submit调用$scope.getWeather之前,$scope.temp和$scope.description没有值。我可以在提交后记录这些值,但无法更新视图以显示它们$test演示了当调用$scope.getWeather时,我的视图将只显示“这是默认值”,而不会更新为“这是新值”。谢谢Javascript ng submit调用工厂函数后如何更新视图?,javascript,angularjs,Javascript,Angularjs,在ng submit调用$scope.getWeather之前,$scope.temp和$scope.description没有值。我可以在提交后记录这些值,但无法更新视图以显示它们$test演示了当调用$scope.getWeather时,我的视图将只显示“这是默认值”,而不会更新为“这是新值”。谢谢 angular.module('forecastCtrl', ['weatherService']) .controller('forecastController', function($ht
angular.module('forecastCtrl', ['weatherService'])
.controller('forecastController', function($http, $scope, Weather) {
//var vm = this;
$scope.test = 'This is a DEFAULT value';
$scope.getWeather = function(postData) {
Weather.get(postData)
.success(function(data) {
console.log(data);
$scope.test = 'This is the NEW value';
$scope.temp = data.query.results.channel.item.condition.temp;
$scope.description = data.query.results.channel.item.condition.text;
})
.error(function(error) {
console.log(error);
});
event.preventDefault();
};
// THIS IS THE FORM
<form name="getWeatherForm" action="#" ng-submit="getWeather(postData)" class="small-10 large-6 small-centered large-centered columns">
<div class="row collapse">
<div class="small-9 columns">
<input type="text" placeholder="Philadelphia, PA" ng-model="postData.city">
</div>
<div class="small-3 columns">
<button type="submit" class="button postfix">Go</button>
</div>
</div>
</form>
//THIS IS THE FACTORY
angular.module('weatherService', [])
.factory('Weather', function($http) {
return {
get: function(location) {
location = location.city;
location = location.replace(/\s/g, '');
// do error handling
return $http.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22'+location+'%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
}
}
});
// THE VIEW
<main id="content">
<div class="row">
<div class="small-12 large-12 columns">
<section class="wrapper">
{{ test }}
<h1>{{ forecast.temp }}</h1>
<h2>{{ forecast.description }}</h2>
</section>
</div>
</div>
角度模块('forecastCtrl',['weatherService'])
.controller('forecastController',函数($http,$scope,Weather){
//var vm=这个;
$scope.test='这是一个默认值';
$scope.getWeather=函数(postData){
Weather.get(postData)
.成功(功能(数据){
控制台日志(数据);
$scope.test='这是新值';
$scope.temp=data.query.results.channel.item.condition.temp;
$scope.description=data.query.results.channel.item.condition.text;
})
.错误(函数(错误){
console.log(错误);
});
event.preventDefault();
};
//这是表格
去
//这是工厂
角度模块('weatherService',[])
.factory('Weather',函数($http){
返回{
获取:函数(位置){
地点=地点。城市;
位置=位置。替换(/\s/g');
//做错误处理
返回$http.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(从%20geo.places(1)%20where%20text%3D%22'+位置+'%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
}
}
});
//景色
{{test}}
{{forecast.temp}}
{{forecast.description}}
代码正在运行,但如果未提供城市或提供了错误的城市,则代表没有假定的属性。因此,可能需要使用ng,因为如果未设置postData.city,代码将失败
event.preventDefault()
可能会导致错误。您能否创建一个SSCCE来演示您的问题?event.preventDefault()的目的是什么?事件从何而来?我们可以看到您的HTML吗?我不确定是否有目的,但我想检查一下。删除它并不能解决问题。我有一个按钮提交此…
对不起,应该指定,绑定$scope.test
的位置如何?尝试将绑定测试作为对象属性,$scope.obj={test:'test'}
,那么obj.test='NEW'
您认为什么没有更新?我没有看到HTML中$scope.test
或$scope.description
的任何用法…类似于{test}
我也这么认为,但我得到了这个错误:所以这不是问题所在。你确定页面没有因为其他javascript而刷新…再次清除表单吗?这就是我对事件的想法。preventDefault()将用于,对吗?它似乎正在工作!只是响应中没有通道属性。请记住在字段中放置一些内容。不会传入占位符。这很愚蠢。我与我的app.routes为我的视图声明控制器时发生冲突,而我也在标题中的一个小范围内保留了它。掌纹
.success(function(data) {
console.log(data);
$scope.test = 'This is the NEW value';
$scope.temp = data.query.results.channel.item.condition.temp;