簡體   English   中英

Angular js ng-click問題

[英]Angular js ng-click issue

我想讀取一個Json對象,然后將Json元素放入我的html(用戶界面)中,問題是,只有當我在按鈕上單擊兩次時,該元素才會出現在視圖中。 這是控制器中的功能:

      $scope.openfile=function ()
     {


    $scope.db.select("items", {
       "id": {
      "value": $routeParams.id,
          },
         }).then(function(results) {
    var parser = new xml2js.Parser();
               fs.readFile(results.rows.item(0).path,             function(err, data) {
       parser.parseString(data, function (err, result) {
       if (err == null)
      {
   $scope.descriptionApp=result.widget.description[0];
   }
     });
     });
      }

html:

       <label class="control-label" for="inputDefault"></label>
        <textarea type="text" class="form-control" id="description"  ng-model="descriptionApp"></textarea>
        <a class="btn btn-primary"  ng-click="openfile()">Open File</a>

從我所看到的,您正在嘗試從angular范圍之外的函數fs.readFile()更新$ scope。 在這種情況下,您需要使用$ scope。$ apply來包裝操作。 做這個:

    fs.readFile(results.rows.item(0).path,function(err, data) {
       parser.parseString(data, function (err, result) {
       if (err == null)
      {$scope.$apply(function(){
   $scope.descriptionApp=result.widget.description[0];
});
   }
     });
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM