Javascript 从控制器中函数内部的指令调用函数时出错
我有两个控制器,我在其中做几乎相同的事情,只改变了其中的一些小东西,我有:Javascript 从控制器中函数内部的指令调用函数时出错,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我有两个控制器,我在其中做几乎相同的事情,只改变了其中的一些小东西,我有: app.controller('CreateSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams,
app.controller('CreateSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams, $timeout,ngDialog) {
$scope.title = "Add Signer";
$scope.button = "Save";
$scope.Signer = {};
$scope.cancel = ngDialog.closeAll;
$scope.saveSigner = function() {
SignerResource.save($scope.Signer);
$.notify({
title: '<strong>Data Updated! </strong>',
message: 'Signer Updated.'
},{
type: 'success',
placement: {
from: 'bottom',
align: 'right'
},
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
}
});
ngDialog.closeAll();
$scope.getModelData();
};
$scope.save = $scope.saveSigner;
}]);
在该控制器中:
app.controller('EditSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams, $timeout,ngDialog) {
$scope.title = "Edit Signer";
$scope.button = "Update";
$scope.Signer = SignerResource.get({
id: $scope.signerId
});
$scope.cancel = ngDialog.closeAll;
$scope.updateSigner = function() {
SignerResource.update($scope.Signer);
$.notify({
title: '<strong>Data Updated! </strong>',
message: 'Signer Updated.'
},{
type: 'success',
placement: {
from: 'bottom',
align: 'right'
},
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
}
});
ngDialog.closeAll();
$scope.getModelData();
};
$scope.save = $scope.updateSigner;
}]);
该函数($scope.getModelData)在其他控制器中多次执行,而且执行起来没有问题。我认为您的指令datapaginator不包含在您的**CreateSignerCtrl**中,但它包含在您的EditSignerCtrl中 您应该检查标记,看看是否在两个控制器中都包含了指令 但是,我确实认为应该将getModelData函数改为服务
阅读。希望我能提供帮助:)CreateSignerCtrl控制器中的$scope似乎不是从您的指令继承的。将getModelData函数移动到服务不是更好吗?可能吧,但该函数是一个标准函数,用于任何解析来自模型或服务的第一个控制器的url的模型,指令未包含在两个控制器中我尝试更新我的视图或我当时拥有的视图我希望看到您的标记如果您可以使用html更新您的问题,我可以更好地帮助您解决问题。是的,伙计,html和脚本;)
app.controller('EditSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams, $timeout,ngDialog) {
$scope.title = "Edit Signer";
$scope.button = "Update";
$scope.Signer = SignerResource.get({
id: $scope.signerId
});
$scope.cancel = ngDialog.closeAll;
$scope.updateSigner = function() {
SignerResource.update($scope.Signer);
$.notify({
title: '<strong>Data Updated! </strong>',
message: 'Signer Updated.'
},{
type: 'success',
placement: {
from: 'bottom',
align: 'right'
},
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
}
});
ngDialog.closeAll();
$scope.getModelData();
};
$scope.save = $scope.updateSigner;
}]);
app.directive('datapaginator', function() {
return {
url: '=a',
controller: function($scope, $http) {
$scope.model = [];
$scope.getModelData = function() {
$http({
url: $scope.url,
method: "GET",
params: {page: $scope.currentpage || 1}
}).success(function(data, status, headers, config) {
if($scope.currentpage > data.last_page){
$scope.currentpage = data.last_page;
$scope.getModelData();
}
$scope.currentpage = data.current_page;
$scope.lastpage = data.last_page;
$scope.generatePages(data.last_page);
$scope.model = data.data;
}).error(function(response) {
$scope.model = response.error;
});
};
});