簡體   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