javascript – 如何设置服务以传递谷歌工作表ID? AngularJS

前端之家收集整理的这篇文章主要介绍了javascript – 如何设置服务以传递谷歌工作表ID? AngularJS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个使用angular的小部件,它使用用户给出的Google Sheet Ids并以漂亮的json格式发布输出.问题是我的代码什么也没做.控制台中没有错误,当我添加ID时没有任何反应

我想问题出在service.js中

  1. angular.module('adf.widget.charts')
  2. .service('chartService',function(){
  3. return {
  4. getUrl: function(key){
  5. var googleSheetkey = key
  6. }
  7. }
  8. return googleSheetkey
  9. });

edit.html(添加工作表ID)

  1. <form role="form">
  2. <div class="form-group">
  3. <label for="sample">Sample</label>
  4. <input type="text" class="form-control" id="sample" ng-model="config.googleSheetkey" placeholder="Enter path">
  5. </div>
  6. </form>

App.js

  1. angular.module('adf.widget.charts',['adf.provider','nvd3'])
  2. .config(function(dashboardProvider){
  3. var widget = {
  4. templateUrl: '{widgetsPath}/charts/src/view.html',reload: true,resolve: {
  5. /* @ngInject */
  6. urls: function(chartService,config){
  7. if (config.key){
  8. return chartService.getUrl(config.googleSheetkey);
  9. }
  10. }
  11. },edit: {
  12. templateUrl: '{widgetsPath}/charts/src/edit.html'
  13. }
  14. };
  15.  
  16. dashboardProvider
  17. .widget('piechart',angular.extend({
  18. title: 'Custom Piechart',description: 'Creates custom Piechart with Google Sheets',controller: 'piechartCtrl'
  19. },widget));
  20. });

调节器

  1. angular.module('adf.widget.charts')
  2. .controller('piechartCtrl',function (chartService,$scope) {
  3. window.onload = function() { init() };
  4. function init() {
  5. Tabletop.init( { key: chartService.googleSheetkey,callback: function(data,tabletop) { console.log(data) },simpleSheet: true } )
  6. }
  7.  
  8.  
  9. });

解决方法

工作示例 here

示例路径:

https://docs.google.com/spreadsheet/pub?hl=en_US\u0026amp;hl=en_US\u0026amp;key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE\u0026amp;output=html

最好的方式是与Promise

使用q框架($q服务)

从控制器中删除逻辑到服务

应用

  1. angular.module('myApp',[]);

服务

  1. angular.module("myApp")
  2. .service("chartService",function($q) {
  3.  
  4. return {
  5. getSpreadsheet: function init(path) {
  6. var deferred = $q.defer();
  7. //if no path use the config key
  8. Tabletop.init({
  9. key: path,callback: deferred.resolve,simpleSheet: true
  10. });
  11. return deferred.promise;
  12.  
  13. }
  14. }
  15. });

调节器

  1. angular.module('myApp')
  2. .controller('piechartCtrl',['$scope','chartService',function($scope,chartService) {
  3. $scope.getSheet = function() {
  4. chartService.getSpreadsheet($scope.googleSheetPath).then(function(data) {
  5. $scope.data = data;
  6. })
  7. }
  8. }]);

的index.html

  1. <!DOCTYPE html>
  2. <html ng-app="myApp">
  3.  
  4. <head>
  5. <link rel="stylesheet" href="style.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  7. <script src="tabletop.js"></script>
  8. <script src="script.js"></script>
  9. </head>
  10.  
  11. <body ng-controller="piechartCtrl">
  12.  
  13. <form role="form" ng-submit="getSheet()">
  14. <div class="form-group">
  15. <label for="sample">Sample</label>
  16. <input type="text" class="form-control" id="sample" ng-model="googleSheetPath" placeholder="Enter path">
  17. <input type="submit" id="submit" value="Submit" />
  18. </div>
  19. </form>
  20. <div>
  21.  
  22. {{data}}
  23. </div>
  24. </body>
  25.  
  26. </html>

猜你在找的JavaScript相关文章