簡體   English   中英

AngularJs出廠時的呼叫控制器功能

[英]AngularJs call controller function from factory

我正在嘗試sample with angularJssample with angularJs嘗試call a factory method並且在工廠方法中,我正在執行ajax callback以從數據庫獲取結果,並且在ajax回調的success event中,我需要調用function in the controller中的一個將result to the UI綁定result to the UIfunction in the controller

我的角度代碼:

 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(); } } ]); 

我的角度工廠代碼:

 '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' }); } ]); 

但是,我無法在控制器中調用該函數。 我也嘗試過使用angular.element(document.getElementById('TasksListCtrl')).scope().BindTasksList(response) 但是,即使那樣也不起作用。

誰能指出我的錯誤?
如何將控制器的$ scope發送到工廠?

您可以利用$http承諾來做到這一點,在工廠中,按如下方式返回承諾

'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;
                });
          }
        }
      }
    ]);
}());

然后,在您的控制器中,您可以訪問返回的$http響應對象:

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

如果可以的話,我也建議在$http$q一起使用$q ,這樣就不需要解析HTTP響應並獲得一個不錯的響應數據對象

nk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM