0

I am having problem with Angularjs view. Somehow i am doing something wrong and don't know where is the problem. Hope someone can help me out.

The problem is {{user.login}} (userRepoInfo.html file) does not get called at all from the controller and if i do console.log it is coming through fine from services. I am definitely doing something wrong between view and controller

I have five files
1) gitHubApiServices.js 
2) gitHubApiController.js
3) userRepoInfo.html
4) app.js
5) index.html

Below is gitHubApiServices.js

  (function() {
                var gitHubApiFactory = function($http) {

                    var factory = {};
                    factory.getUserName = function(userName) {
                        console.log(userName + " " + "This is from Services")
                        return $http.get("https://api.github.com/users/" + userName);
                    };
                    return factory;
                };

                gitHubApiFactory.$inject = ['$http'];

                angular.module('gitHubApp').factory('gitHubApiFactory',gitHubApiFactory);

            }());

Below is gitHubApiController.js

 (function() {

            var gitHubInfoController = function ($scope,gitHubApiFactory) {
                $scope.user = null;
                $scope.gitHubUser = function(userName) {
                    gitHubApiFactory.getUserName(userName)
                        .success(function (data) {
                            $scope.user = data;
                            console.log(data);
                        })
                        .error(function(data, status, headers, config) {
                            $log.log(data.error + ' ' + status);
                        });
                }
            };

            gitHubInfoController.$inject = ['$scope','gitHubApiFactory'];

            angular.module('gitHubApp')
                .controller('gitHubInfoController', gitHubInfoController);
        }());

Below is userRepoInfo.html

 <div data-ng-controller="gitHubInfoController">
                <h1>Github App</h1>
                <input type="text" id="gitUserName" ng-model="gitUser" placeholder="Please enter your GitHub Username">
                <a href="#" id="getUserRepo" ng-click="gitHubUser(gitUser)">Get my Repository</a>

                {{user.login}}
            </div>

Below is app.js

(function() {

    var app = angular.module('gitHubApp', ['ngRoute']);

    app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                controller: 'gitHubInfoController',
                templateUrl: 'app/views/userRepoInfo.html'
            })
            .otherwise( { redirectTo: '/' } );
    });

}());

Below is index.html

<!doctype html>
<html data-ng-app="gitHubApp">
<head>
    <title>Github Repository App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

    <div data-ng-view></div>


<script src="vendor/angular.js"></script>
<script src="vendor/angular-route.js"></script>
<script src="app/app.js"></script>

<script src="app/controllers/gitHubApiController.js"></script>
<script src="app/services/gitHubApiServices.js"></script>
</body>
</html>

Please help me out with this problem. I had spend couple of hours but no luck.

Thank you for your effort

12
  • What error u are getting in console Commented Sep 23, 2014 at 1:39
  • if in my controller i do console.log(user.login) it gives me ReferenceError: user is not defined - somehow it is empty. Commented Sep 23, 2014 at 1:42
  • i have defined my controller. I am very new to angularjs. so might have done it wrong? Commented Sep 23, 2014 at 1:44
  • 1
    Hi sorry it is a typo, it should be $scope.user = null; i have fixed it thanks Commented Sep 23, 2014 at 1:51
  • 1
    console.log(user.login) gave error because user isn't defined, so you must use console.log($scope.user.login) Commented Sep 23, 2014 at 1:53

1 Answer 1

1

I have tried your code with {{user.login}} instead of {{$scope.user.login}} in plinkr and it works fine. It returns my username (or any other info I request from GitHub RestFull Api.

You should not use $scope in the view. Angular wil lfigure out the proper scope for you and inject it to be used by the view.

Have a look at this plunkr: http://plnkr.co/edit/16yyngPoZBgzVvfyWCDq

P.S: I have flattened your folder structure for the sake of simplicity.

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

1 Comment

Hi I see now the problem was in the link-tag i was using. Thank you Ali. Thank you very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.