AngularJS控制器不是一个函数,未定义

时间:2015-10-02 19:12:30

标签: javascript angularjs

嗨,我是角色的新手。我只是想将一些硬编码数据添加到表中,当我尝试这样做时,我遇到了2个错误。 1.未捕获的ReferenceError:未定义LookUpCtrl 2.错误:[ng:areq]参数'lookup.controller'不是函数,未定义

这是我的控制器lookup.controller.js

(function () {

'use strict';

angular
    .module('crm.ma')
    .controller('LookUpCtrl', LookUpCtrl);

function LookupCtrl() {
    var vm = this;

    vm.results = [
        {
            accountId: 1,
            accountName: 'some name',
            address: '201 some st',
            city: 'Columbus',
            state: 'OH',
            zip: 'zip',
            phone: '999-999-9999',
            parentName: 'Parent 1',
            accountType: 'Type 1',
            accountStatus: 'Status 1',
            creditTerm: 'Term 1'
        },
        {
            accountId: 2,
            accountName: 'some name',
            address: '201 some st',
            city: 'Columbus',
            state: 'OH',
            zip: 'zip',
            phone: '999-999-9999',
            parentName: 'Parent 1',
            accountType: 'Type 1',
            accountStatus: 'Status 1',
            creditTerm: 'Term 1'
        }
    ];

}
}());

这是我的观点lookup.html

<div>
<div>Lookup Results</div>
<table>
    <thead>
        <tr>
            <td>Acc. ID</td>
            <td>Acc. Name</td>
            <td>Acc Address</td>
            <td>City</td>
            <td>Zip</td>
            <td>Phone</td>
            <td>Parent Name</td>
            <td>Account Type</td>
            <td>Account Status</td>
            <td>Credit Term</td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="result in vm.results">
            <td>{{ result.accountId }}</td>
            <td>{{ result.accountName }}</td>
            <td>{{ result.address }}</td>
            <td>{{ result.city }}</td>
            <td>{{ result.state }}</td>
            <td>{{ reuslt.zip }}</td>
            <td>{{ result.phone }}</td>
            <td>{{ result.parentName }}</td>
            <td>{{ result.accountType }}</td>
            <td>{{ result.accountStatus }}</td>
            <td>{{ result.accountStatus }}</td>
            <td>{{ result.creditTerm }}</td>
        </tr>
    </tbody>
</table>

如果需要任何进一步的信息,请告诉我。感谢。

2 个答案:

答案 0 :(得分:2)

你有一个错字:

angular
    .module('crm.ma')
    .controller('LookUpCtrl', LookUpCtrl); //This should be LookupCtrl, as your function name is "LookupCtrl"

function LookupCtrl() {}; //Check the camelcase name.

所以,正确的代码是:

angular
    .module('crm.ma')
    .controller('LookUpCtrl', LookupCtrl); 

答案 1 :(得分:0)

您没有正确调用初始化。我不知道是否可以制作没有$ scope的控制器,但我建议你使用它。这是我通常初始化角度控制器的方法。您可以安全地定义该函数,而不是像我所做的那样将其作为参数传递

package WorkIDServerStorage;

public class EmployeeList{
    private Employee[] theEmployee;
    private int arrayEmployee;

    public EmployeeList(){
        theEmployee = new Employee[100];
        arrayEmployee = 0;
    }

    public EmployeeList(int arraySize){
        theEmployee = new Employee[arraySize];
        arrayEmployee = 0;
    }

    public void setTheEmployee(Employee[] inputTheEmployee){
        theEmployee = inputTheEmployee;
    }

    public void setArrayEmployee(int inputArrayEmployee){
        arrayEmployee = inputArrayEmployee;
    }

    public Employee[] getTheEmployee(){
        return theEmployee;
    }

    public int getArrayEmployee(){
        return arrayEmployee;
    }

    public Employee addEmployeeID(Employee employeeAdd){
        return theEmployee[arrayEmployee++] = employeeAdd;
    }

    public Employee deleteEmployeeID(int employeeDelete){
         //Delete an employee record with a 
         //specified record number from the array
    }

    public Employee readEmployeeInfo(int employeeRead){
        //Read the employee data for a specified record number 
        //From the array and display this data to the screen
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        for(int x = 0; x < arrayEmployee; x++){
            sb.append(theEmployee[x].toString()).append("\n");
        }return sb.toString();
    }
}

请记得在html中使用var app = angular.module('crm.ma', []).controller('LookUpCtrl', ['$scope', function($scope){ $scope.results=[ { accountId: 1, accountName: 'some name', address: '201 some st', city: 'Columbus', state: 'OH', zip: 'zip', phone: '999-999-9999', parentName: 'Parent 1', accountType: 'Type 1', accountStatus: 'Status 1', creditTerm: 'Term 1' }, { accountId: 2, accountName: 'some name', address: '201 some st', city: 'Columbus', state: 'OH', zip: 'zip', phone: '999-999-9999', parentName: 'Parent 1', accountType: 'Type 1', accountStatus: 'Status 1', creditTerm: 'Term 1' } ]; }]);