Search code examples
angularjsng-controller

error in ngController (angularJS)


I am getting error called

Argument 'myController' is not a function, got undefined

Here is my Code:

Script

var app = angular.module("app", []);
        app.service("serviceText", function () {
            this.start = function (text) {
                return "service is started, Text is " + text;
            };
            this.end = function (text) {
                return "service is stopped, Text is " + text;
            };
        });
        function myController($scope,serviceText) {
            $scope.call = function () {
                $scope.msg = serviceText.start($scope.txt);
            }
            $scope.stop = function () {
                $scope.msg = serviceText.end($scope.txt);
            }
        }

Html

<body ng-app="app" >
    <div ng-controller="myController">
        <input type="text" name="name" ng-model="txt" />
        <button ng-click="call()" >Start</button>
        <button ng-click="stop()" >Stop</button>
        <br />
        <div>{{msg}}</div>
    </div>
</body>

Please tell me whats wrong in my code


Solution

  • If you are using angular version above 1.3, you should declare the controller like this,

    app.controller('myController', function($scope, serviceText){ 
    
    });
    

    if your angular version is below 1.3 , you should call the controller,

    app.controller('myController', myController)