2015-05-20 149 views
2

我創建了一個AngularJS工廠,在該工廠中,我返回一個具有某些功能屬性的對象。我的目的是在使用這個關鍵字返回的函數中調用另一個函數中的某個函數,但它提供了一個錯誤。我試圖console.log 這個關鍵字,我發現,這個變量保存調用控制器的範圍,其中工廠正在被注入。我該如何處理這種情況。 這個關鍵字應該返回當前返回的對象!AngularJS工廠從返回對象訪問此方法

app.module('admin').factory('login', [function() { 
return { 
    someFunc: function() { 
     return 'abc' 
    }, 
    anotherFunc : function(){ 

     var msg = this.someFunc(); 
     return 'Msg is '+ msg; 

    }, 
    yetAnotherFunc : function(){ 

     var msg = this.someFunc(); 
     return 'Msg is '+ msg; 

    } 
} 
}]).controller(['$scope', 'login', function($scope, login){ 
    login.someFunc(); // Works Fine 
    login.anotherFunc(); // Error : this.someFunc is not a function 
}]) 
+0

不知道這是否是解決方案,但你有沒有試過將它綁定到它? –

+0

您也可以在return語句之外定義函數。 –

+0

綁定到它?如何 –

回答

1

試試這個:

app.module('admin').factory('login', [function() { 
    function someFunc() { 
     return 'abc'; 
    } 

    return { 
     someFunc: someFunc, 
     anotherFunc : function(){ 

      var msg = someFunc(); 
      return 'Msg is '+ msg; 

     }, 
     yetAnotherFunc : function(){ 

      var msg = someFunc(); 
      return 'Msg is '+ msg; 

     } 
    }; 
}]).controller(['$scope', 'login', function($scope, login){ 
    login.someFunc(); 
    login.anotherFunc(); 
}]); 

現在someFunc將在返回的對象的範圍可見。

編輯:

關於this問題,我想你是誤會在Javascript中如何this作品。

this關鍵字只會在某些情況下工作「正常」:

  • 你是用的是new關鍵字
  • 你調用一個綁定功能創建的對象上調用的函數(見the docs
  • 你使用callapply調用一個函數,給它一個上下文(這)

看着Waxolunist的回答,我可以看到你發佈的代碼實際上起作用了,所以你可能正在做其他事情導致錯誤。我猜它看起來是這樣的

$scope.yetAnotherFunc = login.yetAnotherFunc 

這從它的上下文將「分離」 yetAnotherFunc,這就是爲什麼this是不是你希望它是什麼。

+0

基本上我的評論:)但是,這就是它 –

+0

我給你@pablochan。我正在考慮它作爲一個解決方案,但你能回答我的查詢,爲什麼返回對象這個關鍵字展品$範圍或呼叫控制器。 –

+0

@ParamSingh我已經更新了答案。我認爲你發佈的代碼不是你實際做的。 – pablochan

1

這裏是小提琴的解決方案:

https://jsfiddle.net/waxolunist/fcxu5eej/

HTML:

<div ng-app="app"> 
    <div ng-controller="LoginController"> 
     Get a Message: 
     <button ng-click="someFunc()">someFunc</button> 
     <button ng-click="anotherFunc()">anotherFunc</button> 
     <button ng-click="yetAnotherFunc()">yetAnotherFunc</button> 

     <div>Answer: {{answer}}</div> 
    </div> 
</div> 

JS:

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

app.controller('LoginController', function($scope, LoginFactory) { 

    $scope.someFunc = function() { 
     $scope.answer = LoginFactory.someFunc(); 
    } 

    $scope.anotherFunc = function() { 
     $scope.answer = LoginFactory.anotherFunc(); 
    } 

    $scope.yetAnotherFunc = function() { 
     $scope.answer = LoginFactory.yetAnotherFunc(); 
    } 
}); 

app.factory('LoginFactory', [function() { 
    return { 
     someFunc: function() { 
      return 'abc' 
     }, 
     anotherFunc : function(){ 
      var msg = this.someFunc(); 
      return 'Msg is '+ msg; 
     }, 
     yetAnotherFunc : function(){ 
      var msg = this.someFunc(); 
      return 'Another msg is '+ msg; 
     } 
    } 
}]); 

但我真的懷疑,工廠是正確的事使用。也許服務會更適合。但基本上你的代碼工作。

+0

它是如何工作的? –

+0

它不適合您在瀏覽器中使用,還是您想要解釋?我不知道你的其他代碼,但是你發佈的代碼片段正在工作。 – Christian

+0

$ scope.loginFb = fbLogin.initLogin;它在我的其他js文件中。這就是爲什麼它不起作用。解釋在上面的評論中。 –

0

https://jsfiddle.net/6fwu7ghs/

HTML

<div ng-app="test" ng-controller="testController"> 
<input type="button" ng-click="testFunc2()" value="test2" /> 
<input type="button" ng-click="testFunc1()" value="test1" /> 
</div> 

JS

var testModule = angular.module('test',[ ]); 
testModule.controller('common' , function( $scope) { 
$scope.testFunc1 = function(){ 
    alert("test1"); 
    }; 
}); 
testModule.controller('testController', function($scope , $controller){ 
//inherit 
$controller('common', { 
    $scope: $scope 
}); 

$scope.testFunc2 = function(){ 
alert("test2"); 
}; 
}); 

這個怎麼樣的方式。我只是使用控制器'繼承'

你真的需要使用'工廠'嗎?那麼這不是一個答案。