javascript - AngularJS : How to read data of JSON file on button click?

标签 javascript html angularjs json

我想在单击按钮时调用一个函数,其中 HTTP 被调用以从 JSON 文件读取数据,当 HTML 加载来自 JSON 文件的数据时,以下代码工作正常 <ul>但我想在按钮(继续按钮)单击上调用函数,我怎样才能实现这一点?因为当我编码http.get时函数中的方法并调用按钮单击它不起作用。

HTML

 <label class="item">
              <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="submit"  ng-disabled="ForgotPasswordForm.$invalid" >Continue</button>

            </label>
    <ul>
      <li ng-repeat="x in loginData">
        {{ x.result +','+ x.status}}
      </li>
    </ul>

SignupCtrl.js

use strict';
angular.module('User').controller('SignupCtrl', ['$scope', '$http','$state', "SettingService", function($scope,$http, $state, SettingService) {

    /*LoginCtrl code will come here*/
    var vm = this;

 vm.signup = function(){
  $state.go('app.orderlist');
 };


 $http.get("forgotpassword.json").success(function (data) {
      $scope.loginData = data;

  });

}]);

最佳答案

您需要将代码包装在函数内,并使用 ng-click 指令在单击时调用该函数。

vm.getData = function(){
  $http.get("forgotpassword.json").success(function (data) {
      $scope.loginData = data;

  });
}

HTML:

  <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="submit" ng-click="vm.getData()"  ng-disabled="ForgotPasswordForm.$invalid" >Continue</button>

关于javascript - AngularJS : How to read data of JSON file on button click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222588/

相关文章:

javascript - 将所有 <img> 标签替换为 img 替代文本

php - 将两个 href 显示到表中的单独列中

ios - 嵌入 iframe 时的 HTML5 游戏触摸问题

angularjs - 为什么这个 ng-show 指令在模板中不起作用?

javascript - 将 Angular Controller 连接到 View 时出现问题

缩小时出现 Javascript 错误

javascript - 正则表达式匹配阿拉伯语关键字

javascript - 判断一个 div 是否有滚动条

html - 并排放置 2 个 angularjs 日期选择器,而不是顶部和底部

php - 将文本区域的内容转换为数组