0

I want to use a user input value in to controller. for example i want to take a URL and send a HTTP GET request. I have tried the following format , unfortunatly it does not work.

<script>
var app = angular.module('myApp',[]);
app.controller('myCtrl' , function($scope, $http){
$http.get("{{url}}")
.success(function(response){
$scope.data=response.data;
});
});
</script>
0

1 Answer 1

2

Here is the url retrieved from a user's input

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {

  $scope.callAPI = function() {
    console.log($scope.url);
    $http.get($scope.url)
      .success(function(response) {
        $scope.result = response.data;
      });
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  Enter url :
  <input type="text" ng-model="url" />
  <input type="submit" ng-click="callAPI()" />
  
  <div ng-bind="result"></div>
</div>

Note : This will show an error if you entered a wrong url

Sign up to request clarification or add additional context in comments.

1 Comment

No problem mate, don't forget to mark it as the answer if it helped you

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.