[英]ng-click or ng-submit is not invoking controller method
I have just started learning Angular JS and downloaded Dan Wahlin's Customer Manager App for my hands on. 我刚刚开始学习Angular JS,并亲自下载了Dan Wahlin的Customer Manager应用程序 。 I have tried so far to call my Web API to return all records and bind with View which worked well. 到目前为止,我已经尝试调用Web API来返回所有记录并与运行良好的View绑定。 Now I am trying to add a textbox with submit button to search for the entered text, which seems very basic functionality of angular app, but failing to invoke controller method by clicking the submit button: 现在,我试图添加带有提交按钮的文本框来搜索输入的文本,这似乎是angular应用程序的基本功能,但无法通过单击提交按钮来调用控制器方法:
<form ng-submit="getSearchResuls(id)">
<input type="text" data-ng-model="id" class=" novalidate form-control" />
<input type="submit" value="Search" />
</form>
I have also tried: 我也尝试过:
<form >
<input type="text" data-ng-model="id" class=" novalidate form-control" />
<input type="submit" value="Search" onclick="getSearchResuls(id)" />
</form>
Both are not working with my following controller definition (function () { 两者都不符合我的以下控制器定义(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);
}());
I have also tried to add ng-controller in form tag but still with no luck. 我也曾尝试在表单标签中添加ng-controller ,但仍然没有运气。 Can I have your valuable remarks to solve this issue please? 请给我宝贵的意见来解决这个问题?
The function getSearchResuls
needs to be added to your scope. 需要将功能getSearchResuls
添加到您的范围。 So, first off, make sure that '$scope' is listed as an injectParams, and then include $scope as an argument for your CustomersController. 因此,首先,请确保“ $ scope”被列为injectParams,然后将$ scope用作CustomersController的参数。 Then do something like this: 然后执行以下操作:
$scope.getSearchResuls = function (id) {
//the rest of your code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.