简体   繁体   English

Angular js ng-click问题

[英]Angular js ng-click issue

I want to read a Json object, then I put the element of Json in my html (user interface), the problem, that the element appears in the view only when I click twice on the button. 我想读取一个Json对象,然后将Json元素放入我的html(用户界面)中,问题是,只有当我在按钮上单击两次时,该元素才会出现在视图中。 this is the function in the controllers: 这是控制器中的功能:

      $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 : 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>

from what I see, you are trying to update the $scope from a function outside of angular's scope - fs.readFile(). 从我所看到的,您正在尝试从angular范围之外的函数fs.readFile()更新$ scope。 In cases like that you need to use $scope.$apply to wrap the operation. 在这种情况下,您需要使用$ scope。$ apply来包装操作。 Do this: 做这个:

    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