繁体   English   中英

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

[英]ng-click or ng-submit is not invoking controller method

我刚刚开始学习Angular JS,并亲自下载了Dan Wahlin的Customer Manager应用程序 到目前为止,我已经尝试调用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>

两者都不符合我的以下控制器定义(function(){

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-controller ,但仍然没有运气。 请给我宝贵的意见来解决这个问题?

需要将功能getSearchResuls添加到您的范围。 因此,首先,请确保“ $ scope”被列为injectParams,然后将$ scope用作CustomersController的参数。 然后执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM