HTML file is this :
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>HTTP Request</title>
<script src="angularjs"></script>
<script src="appjs"></script>
</head>
<body>
<div ng-controller="myCtrl1">
First Name: <input type="text" ng-model="fname" required>
Last Name: <input type="text" ng-model="lname" required>
<button ng-click="search()">Send HTTP Request</button>
<p>Searching for : {{fname}} {{lname}}</p>
<p>Response Status: </p>
<p>{{status}}</p>
<p>Data: {{data}}</p><br>
<p>Total number of results : {{data.numResults}}</p>
</div>
</body>
</html>
I have written a controller as :
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl1', ['$rootScope', '$http', function($rootScope, $http) {
$rootScope.fname = "";
$rootScope.lname="";
$rootScope.search = function() {
var search_url = "/search?fname=" + $rootScope.fname + "&lname=" + $rootScope.lname;
alert (search_url);
// alert("/search?fname=" + $scope.fname + "&lname=" + $scope.lname);
$http({method:'GET', url:search_url})
.success(function(data, status, headers, config) {
$rootScope.status = status;
$rootScope.data = data;
$rootScope.headers = headers;
$rootScope.config = config;
});
};
}]);
But it shows url in alertbox as : /search?fname=&lname= But when I use $scope everywhere instead of $rootScope, it works fine (alert window shows url properly /search?fname=john&lname=player). Need help to understand $rootScope in details. Thanks.