gpt4 book ai didi

javascript - AngularJs 从工厂调用 Controller 函数

转载 作者:行者123 更新时间:2023-12-03 08:13:50 25 4
gpt4 key购买 nike

我正在尝试 sample with angularJs我正在尝试call a factory method在工厂方法中,我正在做 ajax callback从数据库和 success event 中获取结果ajax回调中,我需要调用function in the controller绑定(bind)result to the UI .

我的 Angular 代码:

angular.module('myApp.controllers', [])
.controller('TasksListCtrl', ['$scope', '$rootScope', '$routeParams', 'Task',
function($scope, $rootScope, $routeParams, Task) {
debugger;

//factory call
Task.query({
MobileNumber: $routeParams.MobileNumber,
ClientCode: $routeParams.ClientCode
});

$rootScope.UserMobileNumber = $routeParams.MobileNumber;

$scope.BindTasksList = function(resultData) {
debugger;
$scope.Tasks = resultData;
$scope.$apply();
}

}
]);

我的 Angular 工厂代码:

'use strict';

(function() {

function GetTasks(MobileNumber, ClientCode) {
debugger;
$.ajax({
url: '/api/TasksAPI/GetTasksList',
type: 'GET',
datatype: 'json',
data: {
'MobileNumber': MobileNumber,
'ClientCode': ClientCode
},
success: function(response) {
debugger;
$scope.BindTasksList(response);
},
error: function(xhr) {}
});
};

angular.module('myApp.DbScripts', [])
.factory('Task', [
function() {
return {
query: function(data) {
debugger;
GetTasks(data.MobileNumber, data.ClientCode);
}
}
}
]);
}());

我的app.js代码:

'use strict';

angular.module('myApp', [
'ngTouch',
'ngRoute',
'ngAnimate',
'myApp.controllers',
'myApp.DbScripts'
]).
config(['$routeProvider',
function($routeProvider) {
debugger;
$routeProvider.when('/tasks/:MobileNumber/:ClientCode', {
templateUrl: 'partials/tasks-list.html',
controller: 'TasksListCtrl'
});
$routeProvider.when('/tasks/:taskId', {
templateUrl: 'partials/task-details.html',
controller: 'TaskDetailCtrl'
});
$routeProvider.when('/tasks/:taskId/status', {
templateUrl: 'partials/task-completion-details.html',
controller: 'TaskCompletionDetailCtrl'
});
$routeProvider.when('/tasks/:taskId/route', {
templateUrl: 'partials/route-details.html',
controller: 'RouteDetailCtrl'
});
$routeProvider.otherwise({
redirectTo: '/tasks'
});
}
]);

但是,我无法调用 Controller 中的函数。我也尝试过 angular.element(document.getElementById('TasksListCtrl')).scope().BindTasksList(response) 。但即使这样也行不通。

谁能指出我犯的错误吗?
如何将 Controller 的$scope发送到工厂?

最佳答案

您可以通过利用 $http promise 来做到这一点,在您的工厂中,按如下方式返回 promise

'use strict';

(function() {

function GetTasks(MobileNumber, ClientCode) {

};

angular.module('myApp.DbScripts', [])
.factory('Task', [
function($http) {
return {
query: function(data) {
return $http({
url: '/api/TasksAPI/GetTasksList',
method: 'GET',
params: {
'MobileNumber': data.MobileNumber,
'ClientCode': data.ClientCode
}
}).then(function(result) {
return result;
});
}
}
}
]);
}());

然后在您的 Controller 中,您可以访问返回的 $http 响应对象:

      //factory call
Task.query({
MobileNumber: $routeParams.MobileNumber,
ClientCode: $routeParams.ClientCode
}).then(function(resp) {
// access $http resp here and attach to $scope.Tasks
});

如果可以的话,我建议也使用 $q$http ,这样你就不需要解析 http 响应并获得一个不错的小信息返回响应数据对象

plnk

关于javascript - AngularJs 从工厂调用 Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34036224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com