简体   繁体   English

$ http.get之后视图未更新

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

I know there has been a lot of posts on this and I've tried their solutions but it still does not work. 我知道有很多关于此的文章,我已经尝试了他们的解决方案,但仍然无法正常工作。

Basically I need to get a list of countries from my database and display it on the html page on document load 基本上,我需要从数据库中获取国家列表,并在文档加载时将其显示在html页面上

Below is my controller 下面是我的控制器

$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();

And on my html page 在我的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}}

Your help is much appreciated! 非常感谢您的帮助! Thank you! 谢谢!

Try to use: 尝试使用:

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

Input value 输入值

value="{{item.country_code}}"

Bind the text box by the following way: 通过以下方式绑定文本框:

<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" />

You need to use $scope.$apply() function so replace $scope.country_options = data line with below 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