How to setup service to pass google sheet IDs? AngularJS

后端 未结 3 943
无人及你
无人及你 2021-01-18 04:19

I am building a small widget using angular that takes Google sheet Ids given by user and publishes the output in nice json format. The problem is that my code does nothing.

3条回答
  •  长情又很酷
    2021-01-18 04:28

    Your code is very unclear. If you want to build a proper service that you can set in the config phase you need a Provider instead, like so:

    App.js

    angular.module("adf.widget.charts", ["services"])
    .config(["chartProvider", function (chartProvider) {
        chartProvider.setKey(1232456);
    }]);
    

    Service.js

    angular.module("services", [])
    .provider("chart", [function () {
        var googleSheetkey = null;
    
        this.setKey = function (newKey) {
            googleSheetkey = newKey;
        };
    
        function GoogleSheetkey(key) {
            this.googleSheetkey = key;
        }
    
        this.$get = [function () {
            return new GoogleSheetkey(googleSheetkey);
        }];
    }]);
    

    Controller.js

    angular.module("adf.widget.charts")
       .controller("piechartCtrl", ["$scope", "chart",
            function ($scope, chart) {
                $scope.key = chart.googleSheetkey;
          }
        ]);
    

    Index.html

    
      
    {{key}}

    Plunker

    Also, I really suggest you look at this thread regarding Providers, Factories and services:

    AngularJS: Service vs provider vs factory

    Hope that will help you a little bit.

提交回复
热议问题