繁体   English   中英

$ http.get之后视图未更新

[英]View is not updated after $http.get

我知道有很多关于此的文章,我已经尝试了他们的解决方案,但仍然无法正常工作。

基本上,我需要从数据库中获取国家列表,并在文档加载时将其显示在html页面上

下面是我的控制器

$scope.country_options = [];

function get_countries() {
  var url = "/dashboard-api/api/country/";
  var defer = $q.defer();

  $http.get(url)
    .then(function(response) {
      defer.resolve(response.data.result);
    });

  return defer.promise;
}

function init() {
  var promise = get_countries();
  promise.then(function(data) {
    $scope.country_options = data;
  })
  console.log($scope.country_options);
}

init();

在我的html页面上

<input class="form-check-input" type="checkbox" ng-repeat="item in country_options" value="item.country_code"
       ng-model="country[item.country_code]">
&nbsp;{{item.name}}

非常感谢您的帮助! 谢谢!

尝试使用:

$timeout( function(){ 
   $scope.country_options = data
}, 0);

输入值

value="{{item.country_code}}"

通过以下方式绑定文本框:

<input type ="text" ng-bind="item.country_code" />
or
<input type="text" ng-value="item.country_code" />
or
<input type="text" value="{{item.country_code}}" />
or
<input type="text" ng-model="item.country_code" />

您需要使用$ scope。$ apply()函数,因此将$ scope.country_options = data行替换为以下代码

 $scope.$apply(function() { $scope.country_options = data }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM