AngularJS Factory可以相互交互

时间:2015-03-03 11:37:51

标签: angularjs

我有一个上传的AngularJS Factory,我正在尝试让这个进程触发一个名为progressState的函数,但是它无效。

有没有让这个工作?

myApp.factory('fooFactory', function() {

    return {
        upload: function(foo){
            upload(foo)
            .progress(evt){
               progressState(evt, state);
            }
        },
        progressState: function(evt, state){
            return fooVar;
        }
    };
});

2 个答案:

答案 0 :(得分:0)

使用this

外部回报

 var self = this;

内部回报

self.progressState(evt, state);

答案 1 :(得分:0)

我已经嘲笑了我无法访问你的上传功能,这有用吗



var myApp = angular.module('myApp', []);

myApp.factory('fooFactory', function() {
    var factory = {};  
    factory.upload = function(foo){
          console.log ('factory : ' , factory)
          factory.progressState();
        }
  
    factory.progressState = function(){
          console.log('progress state called')
    }
    
    return factory;
});

myApp.controller('myCtrl', ['$scope', 'fooFactory', function($scope, fooFactory){
  $scope.test = 'controller active';
 
  fooFactory.upload();
  
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <p>{{test}}</p>
  </div> 
</div>
&#13;
&#13;
&#13;