简体   繁体   English

从AngularJS中的控制器功能发送数据以进行查看

[英]Send data to view from controller function in AngularJS

I'm a little new to Angular, and I think I'm missing a key step in sending data from the controller to be able to display in my view. 我对Angular有点新意,我认为我错过了从控制器发送数据以便能够在我的视图中显示的关键步骤。

The functionality I'm looking to create is that a user inputs search requirements into a form and clicks the submit button. 我想要创建的功能是用户将搜索需求输入到表单中并单击提交按钮。 Clicking the submit button fires a function in the controller ( getQuote() ), which makes an API call and returns the API response to be displayed in the view the form redirects to. 单击提交按钮将触发控制器中的一个函数( getQuote() ),该函数进行API调用并返回API响应,以便在表单重定向到的视图中显示。 I've been able to have my function print the response to the console successfully, but the view remains blank. 我已经能够让我的功能成功打印出对控制台的响应,但视图仍然是空白的。

Here is my code: 这是我的代码:

Jade template for search form 搜索表单的玉模板

div(ng-controller="StockCtrl")
    form(action="/#/stock/quote")
        div(class="form-group")
            h3 Enter your stock symbol
            input(type="text" placeholder="AAPL ..." class="form-control input-lg" ng-model="formData.symbol")
            br
            h3 What time period are you interested in?
            input(type="date" id="startdate" class="form-control input-lg" ng-model="formData.startdate")
            br
            button(type="submit" class="btn btn-primary btn-lg" ng-click="getQuote()") Compare

Controller 调节器

var stockrControllers = angular.module('stockrControllers', []);

stockrControllers.controller('StockCtrl', ['$scope', '$http', '$routeParams',
    function ($scope, $http, $routeParams) {

  $scope.stockData = [];
  $scope.formData = [];

  $scope.getQuote = function(startdate){
    var symbol = $scope.formData.symbol;
    var startdate = new Date($scope.formData.startdate);
    var startday = startdate.getDate();
    var startmonth = startdate.getMonth() + 1;
    var startyear = startdate.getFullYear();
    var enddate = new Date(startdate);
    enddate.setDate(startday + 5);
    var endday = enddate.getDate();
    var endmonth = enddate.getMonth() + 1;
    var endyear = enddate.getFullYear();

    //format dates to work with API call
    startdate = startyear + "-" + startmonth + "-" + startday;
    var enddate = endyear + "-" + endmonth + "-" + endday;

    $http({
      method: 'GET',
      url: 'api/stock/' + symbol + '/' + startdate + '/' + enddate
    }).then(function successCallback(response) {
        $scope.stockData = response;
        console.log($scope.stockData);
      }, function errorCallback(response) {
        console.log('Error:' + response);
    });

  };

}]);

Jade template for view to print data 用于打印数据的视图的玉模板

h1 Stock Data

div(ng-controller="StockCtrl")
    div(ng-repeat="stock in stockData")
        p stock data: {{stock}}

When I don't have my API calls wrapped in a function, and instead just use an if statement (like if($scope.formData){..} ), I can display the API response just fine in the view. 当我没有在函数中包含我的API调用时,而只是使用if语句(如if($scope.formData){..} ),我可以在视图中显示API响应。 However, putting the call in a function however is preventing the data from making it to the page. 但是,将调用置于函数中会阻止数据进入页面。

response.data contain actual contents. response.data包含实际内容。 so try replacing your api call code as shown below 所以请尝试替换您的api呼叫代码,如下所示

$http({
      method: 'GET',
      url: 'api/stock/' + symbol + '/' + startdate + '/' + enddate
    }).then(function successCallback(response) {
        $scope.stockData = response.data;
        console.log($scope.stockData);
      }, function errorCallback(response) {
        console.log('Error:' + response);
    });

You have declared two separate div(ng-controller="StockCtrl") and each one has it's own $scope . 您已经声明了两个单独的div(ng-controller="StockCtrl") ,每个div(ng-controller="StockCtrl")都有自己的$scope If you merge them into a single div, then $scope.stockData will be visible: 如果将它们合并为一个div,则可以看到$scope.stockData

div(ng-controller="StockCtrl")
    form(action="/#/stock/quote")
        div(class="form-group")
            h3 Enter your stock symbol
            input(type="text" placeholder="AAPL ..." class="form-control input-lg" ng-model="formData.symbol")
            br
            h3 What time period are you interested in?
            input(type="date" id="startdate" class="form-control input-lg" ng-model="formData.startdate")
            br
            button(type="submit" class="btn btn-primary btn-lg" ng-click="getQuote()") Compare

    div(ng-repeat="stock in stockData")
        p stock data: {{stock}}

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

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