ng-click 或 ng-submit 不调用控制器方法

ng-click or ng-submit is not invoking controller method

本文关键字:控制器 方法 调用 ng-submit ng-click      更新时间:2023-09-26

>我刚刚开始学习Angular JS,并下载了Dan Wahlin的客户经理应用程序为我动手。到目前为止,我已经尝试调用我的 Web API 来返回所有记录并与运行良好的 View 绑定。现在我正在尝试添加一个带有提交按钮的文本框来搜索输入的文本,这似乎是 angular 应用程序的非常基本功能,但无法通过单击提交按钮调用控制器方法:

    <form ng-submit="getSearchResuls(id)">
        <input type="text" data-ng-model="id" class=" novalidate form-control" />
        <input type="submit" value="Search"  />
    </form>

我也尝试过:

        <form >
        <input type="text" data-ng-model="id" class=" novalidate form-control" />
        <input type="submit" value="Search" onclick="getSearchResuls(id)" />
    </form>

两者都不适用于我的以下控制器定义 (函数 () {

var injectParams = ['$location', '$filter', '$window',
                    '$timeout', 'authService', 'dataService', 'modalService'];
 var CustomersController = function ($location, $filter, $window,
    $timeout, authService, dataService, modalService) {
function getSearchResuls(id) {
        dataService.getSearchResults(id, vm.currentPage - 1, vm.pageSize)
            .then(function (data) {
                vm.totalRecords = data.totalRecords;
                vm.customers = data.results;
                filterCustomers(''); //Trigger initial filter
                $timeout(function () {
                    vm.cardAnimationClass = ''; //Turn off animation since it won't keep up with filtering
                }, 1000);
            }, function (error) {
                $window.alert('Sorry, an error occurred: ' + error.data.message);
            });
    }
};
CustomersController.$inject = injectParams;
angular.module('customersApp').controller('CustomersController', CustomersController);
}());

我也尝试在表单标签中添加ng控制器,但仍然没有运气。请问你能有你宝贵的意见来解决这个问题吗?

需要将

函数getSearchResuls添加到您的作用域中。因此,首先,确保"$scope"被列为 injectParams,然后将$scope作为 CustomersController 的参数。然后执行以下操作:

$scope.getSearchResuls = function (id) {
   //the rest of your code here
}