0

I'm very new to AngularJS, how can I pass input scope from first controller to the second controller for the data $scope.requestURL

I've search about the service method but I have no idea how to apply it.

    .controller('ZipController', ['$scope', function($scope) {
    $scope.zipCode = '10028';

    $scope.setURL = function() {
    $scope.requestURL = 'http://congress.api.sunlightfoundation.com/legislators/locate?zip=' + $scope.zipCode + '&apikey=xxxxx';
    };

    }])

   .controller('ListController', ['$scope', '$http', 
   function($scope, $http) {
   $http.get($scope.requestURL).success(function(data) {
   console.log(data.results);
   $scope.congress = data.results;
   });
   }]);
3
  • Say what you're trying to do without talking about code so we can understand your problem better. Commented Oct 19, 2015 at 20:51
  • Please just take the time to just learn how to use services. They really help organize and encapsulate your code. Maintainability and separation of concerns and all of that jazz. Commented Oct 19, 2015 at 20:52
  • Hello Steve, I just like to know how usually URL request are assemble from input field and go into &http request Commented Oct 19, 2015 at 21:10

2 Answers 2

1

Here is a quick solution: ..you don't have to use the $http core service for your case:

You can also read more about angular's constant service..

(function(angular) {
    var app = angular.module('myServiceModule', []);
    app.controller('ZipController', function($scope, myService) {
        $scope.zipCode = '10028';
        myService.setFunc($scope.zipCode);
        myService.zipCode = $scope.zipCode;
    });
    app.controller('ListController', function($scope, myService) {
        $scope.requestURL = myService.getFunc();
    });
    app.factory('myService', function() {
        var zipCode;
        var setFunc = function(zip) {
            zipCode = zip;
        };
        var getFunc = function() {
            return 'http://congress.api.sunlightfoundation.com/legislators/locate?zip=' + zipCode + '&apikey=xxxxx'
        };
        return {
            zipCode: zipCode,
            setFunc: setFunc,
            getFunc: getFunc
        };
    });
})(window.angular);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Pro... I will try this code... I'm a lil bit slow, your code should give me the insight.
you should accept it, if it is the right solution for you
0

Try setting it to $rootScope.requestURL and access it from the second controller.

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.