[英]How to call function in AngularJS controller with $scope method?
I have a function inside my controller that I want to be able to call from a $scope method so I can reuse my code only within the controller. 我希望在控制器内部有一个函数,该函数可以从$ scope方法调用,以便仅在控制器内重用代码。 I use the function to clear some input fields.
我使用该功能清除一些输入字段。 I would like to call after a user is inserted as well as I would like to assign it to the a "Clear" button click.
我想在插入用户后调用,也希望将其分配给“清除”按钮单击。 Calling the function after inserting works just fine but assigning the function to the method isn't.
插入后调用函数就可以了,但是不能将函数分配给方法。
If I assign the function directly to the $scope.clearUser method it works but then I can't call it after inserting a user. 如果我直接将函数分配给$ scope.clearUser方法,则它可以工作,但在插入用户后无法调用它。
function clearUser() {
$scope.EmployeeNumber = '';
$scope.isAdmin = false;
};
$scope.clearUser = clearUser();
//also tried $scope.clearUser = this.clearUser();
//$scope.clearUser = function() {
// $scope.EmployeeNumber = '';
// $scope.isAdmin = false;
//};
<button ng-click="clearUser()" class="btn btn-default">
<i class="fa fa-times"></i> Clear
</button>
Omit the ()
to pass the function: 省略
()
以传递函数:
$scope.clearUser = clearUser;
Or, create it all at once: 或者,一次创建所有内容:
var clearUser = $scope.clearUser = function() {
}
try to change, 尝试改变
$scope.clearUser = clearUser(); $ scope.clearUser = clearUser();
to 至
$scope.clearUser = clearUser; $ scope.clearUser = clearUser;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.