简体   繁体   English

AngularJS:如何在单击按钮时读取JSON文件的数据?

[英]AngularJS : How to read data of JSON file on button click?

I want to call a function on button click in which HTTP called to read data from JSON file , Following code is working fine when HTML loads the data from JSON file is loading on <ul> but I want to call function on button (Continue button) click , How can I achieve this? 我想在按钮单击上调用一个函数,其中调用了HTTP来从JSON文件读取数据,当HTML从<ul>上加载JSON文件中的数据加载HTML时,以下代码可以正常工作,但是我想在按钮上调用函数(继续按钮)点击,如何实现? because when I encoded http.get method in a function and calls on button click it doesn't work . 因为当我在函数中对http.get方法进行编码并调用按钮单击时它不起作用。

HTML 的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 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;

  });

}]);

You need to wrap the code inside a function and call the function on click using the ng-click directive. 您需要将代码包装在一个函数中,然后使用ng-click指令在单击时调用该函数。

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

  });
}

HTML: HTML:

  <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="submit" ng-click="vm.getData()"  ng-disabled="ForgotPasswordForm.$invalid" >Continue</button>
vm.getData= function(){
$http.get("forgotpassword.json").success(function (data) {
  $scope.loginData = data;
 });
};

In html use 在html中使用

ng-click="vm.getData()"

you can modify your button like this 您可以像这样修改按钮

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM