0

So I have an html page that should dynamically make table data appear using AngularJS and smart-table. This a quick summary of what it looks like:

<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>
...

And I have AngularJS code that does a simple $http get request and return a dataset.

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 });
        };
    }]);

For some reason no matter how I define the function searchEmp whether be like above or like function searchEmp() {...} it is always undefined. I don't know what I'm doing wrong. It looks like every other function I've ever made in AngularJS but it won't work.

The exact error I'm getting is this:

Uncaught ReferenceError: searchEmp is not defined
    onkeyup @ Startup.aspx.83
1
  • 2
    Why are you using onkeyup when you have Angular? Have you tried using ngKeypress? Commented Dec 9, 2015 at 16:43

2 Answers 2

1

Try this, using ng-keyup instead of 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
      });
    };
  }
]);
<body ng-app="startApp">
  <div ng-controller="employeeCtrl">
    <tbody>
      <tr>
        <td>
          <input type="text" id="FirstName" size="10" ng-keyup="searchEmp()" />
        </td>
        </td>
      </tr>
    </tbody>
  </div>
  <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>
    </table>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

0

As tymeJV pointed out, onkeyup is a javascript event. You should use ngKeypress which will look for the method on $scope. That should fix your exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.