错误:参数'ContactController'不是函数,未定义

时间:2015-02-12 13:11:08

标签: javascript angularjs controller

我正在尝试使用angularjs在我的简单网页应用中创建一个小控制器,但是我收到了这个错误:

Error: Argument 'ContactController' is not a function, got undefined

这就是我到目前为止所做的:

angular(ContactsController.js):

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

mainApp.ContactController = ("ContactController", ["$scope", function($scope) {
    $scope.contatti = {};
    $scope.contatti.contacts = ["test.one@gmail.com", "test.two@gmail.com"];

    $scope.contatti.addContacts = function () {
        $scope.contatti.contacts.push($scope.newcontact);
        $scope.contatti.newcontact = "";
    };

}]);

HTML:

    <!DOCTYPE html>
<html data-ng-app="mainApp" data-ng-controller="ContactController">

<head>
    <meta charset="utf-8">
    <script src="jquery-1.11.2.js"></script>
    <script src="angular-1.3.12/angular.js"></script>
    <script type="text/javascript" src="angular-1.3.12/angular.js"></script>
    <script type="text/javascript" src="angular-1.3.12/angular-cookies.js"></script>
    <script type="text/javascript" src="angular-1.3.12/angular-resource.js"></script>
    <script type="text/javascript" src="angular-1.3.12/angular-sanitize.js"></script>

    <link type="text/css" rel="stylesheet" href="ukit-2.16.2/css/uikit.almost-flat.css" />
    <script src="ContactsController.js"></script>
    <link type="text/css" rel="stylesheet" href="main.css" />
</head>

<body>

    <div>
        Email:<input type="text" ng-model="contatti.newcontact" />

        <button ng-click="addContacts()">Add</button>

        <h2>Contacts</h2>
        <ul>
            <li ng-repeat="contact in contatti.contacts"> {{ contact }} </li>
        </ul>

    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

应该是这样的: -

mainApp.controller("ContactController", ["$scope", function($scope) {
    $scope.contatti = {};
    $scope.contatti.contacts = ["test.one@gmail.com", "test.two@gmail.com"];

    $scope.contatti.addContacts = function () {
        $scope.contatti.contacts.push($scope.newcontact);
        $scope.contatti.newcontact = "";
    };

}]);