1

I'm trying to learn AngularJS, and I'm wondering if I can do this or not?: Here is my code:

(function() {
    function InfoController($scope,$element){
        $scope.items = data['data']; //data in Option controller
    }
    function OptionController($scope,$element,$http){
        $element.find(".list-group-item").click(function() {
          var a = $(this).text();
            $http({
                url: 'hand',
                method: "GET",
                params: {a: a},
            }).success(function(data){
               console.log(data['data'][0]);
            });
        });
    }
    angular.module('testModule', [])
        .controller('InfoController', InfoController)
        .controller('OptionController', OptionController);
})();

I'm new in AngularJS and I don't know how to pass values data['data'] from $http.get in OptionController to InfoController, so please tell me how can I do that :)

1 Answer 1

3

Yes, you would do so with a service docs

app.service('dataService', function(http) {
  this.data;
  var self = this;

  this.getData = function() {
      return $http.get('/data').then(function(resp) {
        self.data = resp.data;
        return self.data
      })
  }

you would then pass in dataService to both controllers

$scope.data = dataService.getData()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.