简体   繁体   English

angularjs-如何使用html select

[英]angularjs - how to use html select

Hi I have a select option in html showing Apple, Google and Amazon 嗨,我在html中有一个选择选项,显示了Apple,Google和Amazon

 <div class="col-md-9" id="line-chart" ng-controller="LineCtrl"> <div class="panel panel-default" > <select ng-model="selec"> <option value="aapl">AAPL(Apple)</option> <option value="goog">GOOG(Google)</option> <option value="amzn">AMZN(Amazon)</option> </select> <div class="panel-body"> <canvas id="line" class="chart chart-line chart-xl" data="data" labels="labels" legend="true" click="onClick()" series="series"></canvas> </div> </div> </div> 

I can show the Apple data as a default with Angualr JS 我可以使用Angualr JS将Apple数据显示为默认值

 (function () { 'use strict'; var app = angular.module('examples', ['chart.js', 'ui.bootstrap']); app.controller('LineCtrl', ['$scope', '$http', function($scope, $http) { $http.get('stock/aapl.json').success(function(data) { $scope.selec = 'aapl'; //default as age $scope.series = ['Tweet Mood', 'Stock Market']; $scope.stocks = data; $scope.data = [[], []]; $scope.createGraphArray = function() { for (var i = 0; i < $scope.stocks.length; i++) { $scope.data[0].push($scope.stocks[i].percentage1); $scope.data[1].push($scope.stocks[i].percentage2); } }; $scope.createGraphArray(); 

How can I use other select options (google and amazon) taking the data from json files. 如何使用其他选择选项(Google和Amazon)从json文件中获取数据。 json files are already exist. json文件已经存在。 (eg . goog.json, amzn.json) Should I make another controller in javascript? (例如goog.json,amzn.json)我应该在javascript中制作另一个控制器吗? or any other solution? 或其他解决方案?

Thanks in advance. 提前致谢。

All you really need to do is wrap the request code in a function. 您真正需要做的就是将请求代码包装在一个函数中。 This allows you to set the url based on $scope.selec 这使您可以基于$scope.selec设置网址

$scope.selec = 'aapl'; // keep this out of function

$scope.getChartData = function(){
    var url = 'stock/' + $scope.selec + '.json' ;

    $http.get(url).success(function(data) {
        // same code as before except as noted above
     });
}
// call function when controller intializes
$scope.getChartData();

Now in html add the new function as change handler in ng-change 现在在html中将新功能添加为ng-change更改处理程序

<select ng-model="selec" ng-change="getChartData()">

You can use an ng-change to listen on changes of the selection. 您可以使用ng-change来监听选择的更改。

And make it call a function in your controller. 并使其在您的控制器中调用一个函数。

Based on the input of that function you generate a new graph ? 根据该函数的输入,您将生成一个新图?

Is that something your trying to build 那是你试图建立的东西吗

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

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