Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js "typeError: V2.example is not a function" error

Tags:

html

angularjs

i am new in angular.js and i am following a tutorial that was made about a year ago i am trying to create a search function that takes the input and search for it in Github.com The HTML code is :

<!DOCTYPE html>
<html ng-app="github">

<head>
    <script src="angular.min.js"></script>
    <script src="script.js"></script>
</head>

<body ng-controller="main">
    <h1>{{message}}</h1>
    <p> {{username}} </p>
    <form name="search">
        <input type="search" placeholder="enter name" ng-model="username">
        <button type="submit" ng-click="search(username)">search</button>
    </form>
    <div>
        <h1> {{user.login}} </h1>
        <img src="http://www.gravatar.com/avatar/{{user.gravatar_id}}">
        <p> {{user.type}} </p>
    </div>
</body>

</html>

and the JS code :

// Code goes here
(function () {
    var app = angular.module("github", []);
    var main = function ($scope, $http) {
        var onComplete = function (response) {
            $scope.user = response.data;
        };
        var onError = function (reasone) {
            $scope.error = "no can";
        };
        $scope.search = function (username) {
            $http.get("http://api.github.com/users/" +username).then(onComplete, onError);
        };
        $scope.message = "Git hub viewer";
    };
    app.controller("main", ["$scope", "$http", main]);
}());

this gives me an error >>> TypeError: v2.search is not a function <<< for help: i got a problem like that and the solution was not to use "main" function in global type but "search" is not global i guess .. hope this help

here is the codepen link:

http://codepen.io/ToBeM12/pen/vGvwzo

like image 622
Mahmoud Mabrouk Avatar asked Nov 27 '22 04:11

Mahmoud Mabrouk


2 Answers

the function has the same name as another variable and is conflicting in $scope

In your case just replace $scope.search to $scope.searchuser since $scope.search is already defined..

like image 146
Rex Adrivan Avatar answered Dec 09 '22 20:12

Rex Adrivan


I've seen issues where the form name is the same as a scope function or variable name. Try renaming the form to "searchForm" to avoid the conflict with $scope.search.

like image 39
Integrator Avatar answered Dec 09 '22 20:12

Integrator