angular 直接将 $scope 参数从视图发送到服务或工厂
angular directly send $scope parameter from view to service or factory
我有一个工厂,需要从视图调用工厂。
我想用两个参数调用工厂。
是否可以从模板发送 $scope?
因为,我在多个地方使用同一个工厂。
<input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay($scope, accno)" />
控制器,
$scope.myservice= getAllDetailsService;
服役中,
tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window', function ($rootScope, $resource, $http, $filter, $window) {
return{
getAccountDetailsToDisplay: function ($scope, accountnumber) {
console.log('>>>>>>');
}
};
}]);
Service should be directly depends on scope
, they could be
indirectly depend on each other. If you pass $scope to the service it will become tightly couple to that specific controller.
就像您的情况一样,您只传递 accountnumber
,然后服务将执行所需的操作,例如执行 ajax 调用或从某处获取数据。
工厂
tellerApp.factory('getAllDetailsService', ['$rootScope', '$resource', '$filter', '$window', function($rootScope, $resource, $http, $filter, $window) {
return {
getAccountDetailsToDisplay: function(accountnumber) {
return $http.get('/getAccountDetails?accountnumber=' + accountnumber).then(function(res) {
//here you could do your addtional operation on data.
return res.data; //giving access to data
});
}
};
}]);
控制器
$scope.myservice= getAllDetailsService
//this line will ensure updation in scope
$scope.myservice.accountDetailsToDisplay = getAllDetailsService.accountDetailsToDisplay;
标记
<input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay(accno)"/>
同样在上面的代码中我没有使用 $scope
作为参数,服务方法只会 return 从服务中获取任何数据,无论谁使用服务方法都只能获取数据return 按服务。从服务控制器获取数据后,在其自己的上下文中修改范围。
当然,$scope是控制器的内部上下文,所以你不需要在任何其他地方使用它。
如果你想使用工厂,你应该这样写:
tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window', function ($rootScope, $resource, $http, $filter, $window) {
return{
getAccountDetailsToDisplay: function (accountnumber) {
console.log('>>>>>>');
}
};
}]);
并在您的控制器中调用工厂的方法:
$scope.someMethod = getAllDetailsService.getAccountDetailsToDisplay;
在您看来:
<input name="accnum" ng-blur="myservice.someMethod(accno)" />
我有一个工厂,需要从视图调用工厂。 我想用两个参数调用工厂。 是否可以从模板发送 $scope?
因为,我在多个地方使用同一个工厂。
<input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay($scope, accno)" />
控制器,
$scope.myservice= getAllDetailsService;
服役中,
tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window', function ($rootScope, $resource, $http, $filter, $window) {
return{
getAccountDetailsToDisplay: function ($scope, accountnumber) {
console.log('>>>>>>');
}
};
}]);
Service should be directly depends on
scope
, they could be indirectly depend on each other. If you pass $scope to the service it will become tightly couple to that specific controller.
就像您的情况一样,您只传递 accountnumber
,然后服务将执行所需的操作,例如执行 ajax 调用或从某处获取数据。
工厂
tellerApp.factory('getAllDetailsService', ['$rootScope', '$resource', '$filter', '$window', function($rootScope, $resource, $http, $filter, $window) {
return {
getAccountDetailsToDisplay: function(accountnumber) {
return $http.get('/getAccountDetails?accountnumber=' + accountnumber).then(function(res) {
//here you could do your addtional operation on data.
return res.data; //giving access to data
});
}
};
}]);
控制器
$scope.myservice= getAllDetailsService
//this line will ensure updation in scope
$scope.myservice.accountDetailsToDisplay = getAllDetailsService.accountDetailsToDisplay;
标记
<input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay(accno)"/>
同样在上面的代码中我没有使用 $scope
作为参数,服务方法只会 return 从服务中获取任何数据,无论谁使用服务方法都只能获取数据return 按服务。从服务控制器获取数据后,在其自己的上下文中修改范围。
当然,$scope是控制器的内部上下文,所以你不需要在任何其他地方使用它。 如果你想使用工厂,你应该这样写:
tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window', function ($rootScope, $resource, $http, $filter, $window) {
return{
getAccountDetailsToDisplay: function (accountnumber) {
console.log('>>>>>>');
}
};
}]);
并在您的控制器中调用工厂的方法:
$scope.someMethod = getAllDetailsService.getAccountDetailsToDisplay;
在您看来:
<input name="accnum" ng-blur="myservice.someMethod(accno)" />