登陆错误时没有引用父范围

时间:2017-07-20 00:27:37

标签: javascript angularjs

我有一个Angular指令,如下所示

function parentFunction(parentService) {
    vm.childFunction = function() {
        parentService.funFunction()
          .then(function(response) { /* success */ }, 
                function(response) { /* failure */ });      
    };
}

我想在

访问parentService
function(response) { /* failure */ }

但是当我在那时尝试访问parentService时,它会给我以下错误:

Uncaught ReferenceError: parentService is not defined

如何在function(response) { /* failure */ }部分使用parentService?

1 个答案:

答案 0 :(得分:1)

parentService 应作为参数传递给您的控制器而不是您的功能

.controller('PhotoCtrl', ['parentService', function (parentService) {
      vm.childFunction = function() {
        parentService.funFunction()
          .then(function(response) { /* success */ }, 
                function(response) { /* failure */ });      
    };  
}])