1 ответ

Лучший ответ

Оберните вызов в функцию, чтобы вызов testFactory.testFunction(test_var) вызывался только тогда, когда $scope.testFunction() ...

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

app.controller('MainCtrl', function($scope,testFactory) {
  $scope.name = 'World';
  var test_var = "iceland";
  $scope.testFunction = function() {
    testFactory.testFunction(test_var);
  };
});

app.service('testFactory',function(){

  this.testFunction = function(test_var){
    alert(test_var);
  };

})
3
Anthony Chu 15 Авг 2014 в 03:34