[英]angularjs call a function of a controller inside an event
我在AngularJS中遇到問題,這是因為我在控制器中編寫了一個函數,並且想在事件列表器中調用它,但是我卻沒得到答案!
代碼控制器如下所示:
inputApp.controller('my_controller', ['$scope', '$upload', '$http', '$filter', '$sce', '$timeout',
function ($scope, $upload, $http, $filter, $sce, $timeout) {
$scope.changeBlock = function (block) {
// DO SOMETHING
};
$scope.$on('$locationChangeStart', function($scope, next, current){
// Calling the following function makes an error
$scope.changeBlock('Block_NAME');
$scope.preventDefault();
});
}]);
因此在$ scope。$ on .....內調用該函數會導致錯誤:
錯誤:$ scope.changeBlock不是函數!
您能幫我嗎,我該如何在該列表器中調用我的函數?
問候韋爾
您混合了事件處理程序的簽名,並錯誤地覆蓋了$ scope變量。 事件處理程序的第一個參數是“ event”而不是“ $ scope”:
這應該工作:
inputApp.controller('my_controller', ['$scope', '$upload', '$http', '$filter', '$sce', '$timeout',
function ($scope, $upload, $http, $filter, $sce, $timeout) {
$scope.changeBlock = function (block) {
// DO SOMETHING
};
// before:
// $scope.$on('$locationChangeStart', function($scope, next, current){
// now:
$scope.$on('$locationChangeStart', function(event, next, current){
// Calling the following function makes an error
$scope.changeBlock('Block_NAME');
$scope.preventDefault();
});
}]);
將第一個參數名稱: $scope
從$scope.$on('$locationChangeStart', function($scope, next, current){
更改為其他名稱
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.