1

AngularJs ng-click does not respond

I'm trying to use the GitHub Search-API. When someone click the button my Controller should call a search from github here is my code

HTML:

<head>
  <script src="js/AngularJS/angular.min.js"></script>
  <script src="js/Controller/RepoController.js"></script>
</head>
<body ng-controller="RepoController">
 <input type="text" ng-model="param" /><button ng-click="search()">suchen</button>
  <ul>
    <li ng-repeat="repo in repos">
        {{repo.name}}
    </li>
  </ul>
 
</body>

and here is my controller:

var App = angular.module('RepoSearch', []);
 
function RepoController($scope, $http) {
    $scope.repos = [];

    $scope.search = function() {
        $http.get('https://api.github.com/search/repositories?q='+$scope.param).success(function(data) {
            $scope.repos = data['items'];
        }
    }
 
}

but my repos-array is still empty when I swap the $http.get with an alert it is working fine.

3 Answers 3

1

You forgot to close ( after $scope.repos = data['items'];}, and to define ng-app, which in this case, can be in <head> : <head ng-app="RepoSearch"> http://docs.angularjs.org/api/ng.directive:ngApp

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

1 Comment

i defined ng-app in my html tag but i couldnt handle to inster it into code section :D
1

The reason could be cross domain call. Use JSONP to call into github.

$http.jsonp('https://api.github.com/search/repositories?q='+$scope.param+'&callback=JSON_CALLBACK'+).success(function(data) {
            $scope.repos = data['items'];
}

The callback method maybe different so look at these SO posts

Using angularjs JSONP when callback cant be defined

Using JSONP method for $http service in AngularJS

Comments

0

In HTML

<head ng-app="RepoSearch">
      <script src="js/AngularJS/angular.min.js"></script>
      <script src="js/Controller/RepoController.js"></script>
    </head>
    <body ng-controller="RepoController">
     <input type="text" ng-model="param" /><button ng-click="search()">suchen</button>
      <ul>
        <li ng-repeat="repo in repos">
            {{repo.name}}
        </li>
      </ul>

    </body>

in Controller

var App = angular.module('RepoSearch', []);


//bind controller with your app

App.controller('RepoController',RepoController);

function RepoController($scope, $http) {
    $scope.repos = [];

    $scope.search = function() {
        $http.get('https://api.github.com/search/repositories?q='+$scope.param).success(function(data) {
            $scope.repos = data['items'];
        }
    }

}

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.