AngularJS函数未定义

时间:2015-12-09 16:41:14

标签: javascript html angularjs

所以我有一个html页面,应该动态地使用AngularJS和smart-table显示表数据。这是对它的外观的快速总结:

<body ng-app="startApp">
    <div ng-controller="employeeCtrl">
        <tbody>
            <tr>
                <td>
                    <input type="text" id="FirstName" size="10" onkeyup="searchEmp()" /></td>
                </td>
            </tr>
...
<div ng-controller="employeeCtrl">
    <table st-table="employeesCollection" st-safe-src="employeesCollection" class="table table-striped">
        <tbody>
            <tr ng-repeat="employee in employeesCollection">
                <td>{{employee.first}}</td>
            </tr>
        </tbody>
...

我有AngularJS代码执行简单的$ http get请求并返回数据集。

var app = angular.module('startApp', ['smart-table']);

app.controller('employeeCtrl', ['$scope', '$http',
    function ($scope, $http) {

        var first = document.getElementById("FirstName").value;
        var last = document.getElementById("LastName").value;
        var ex = document.getElementById("Extension").value;
        var store = document.getElementById("store");
        var st = store.options[store.selectedIndex].value;
        var number = document.getElementById("storeNo");
        var num = number.options[number.selectedIndex].value;

        $scope.searchEmp = function () {
            $http({
                method: 'GET',
                url: 'getEmployees.aspx',
                params: { f: first, q: last, e: ex, s: st, n: num }
            }).then(function (response) { $scope.employeesCollection = response.data });
        };
    }]);

出于某种原因,无论我如何定义函数searchEmp是否像上面一样或类似function searchEmp() {...},它总是未定义的。我不知道我做错了什么。它看起来像我在AngularJS中做过的所有其他功能,但它不起作用。

我得到的确切错误是:

Uncaught ReferenceError: searchEmp is not defined
    onkeyup @ Startup.aspx.83

2 个答案:

答案 0 :(得分:1)

尝试使用Server代替ng-keyup

keyup
var app = angular.module('startApp', ['smart-table']);

app.controller('employeeCtrl', ['$scope', '$http',
  function($scope, $http) {

    var first = document.getElementById("FirstName").value;
    var last = document.getElementById("LastName").value;
    var ex = document.getElementById("Extension").value;
    var store = document.getElementById("store");
    var st = store.options[store.selectedIndex].value;
    var number = document.getElementById("storeNo");
    var num = number.options[number.selectedIndex].value;

    $scope.searchEmp = function() {
      $http({
        method: 'GET',
        url: 'getEmployees.aspx',
        params: {
          f: first,
          q: last,
          e: ex,
          s: st,
          n: num
        }
      }).then(function(response) {
        $scope.employeesCollection = response.data
      });
    };
  }
]);

答案 1 :(得分:0)

正如tymeJV指出的那样,onkeyup是一个javascript事件。您应该使用ngKeypress来查找$ scope上的方法。这应该可以解决您的异常。