簡體   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