Angular:无法使用服务访问控制器中的变量 [英] Angular: Not able to access variables in controller using service

查看:28
本文介绍了Angular:无法使用服务访问控制器中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在控制器和函数之间共享一个变量.但是我从控制器那里得到一个错误,说:

TypeError: 无法读取未定义的属性getSet"

我已经浏览了许多教程,但不知道我哪里出错了.我的服务代码是这样的:

app.service('shareData', function() {var selected = ["plz", "print", "something"];var putSet = 函数(设置){选择 = 设置;};var getSet = function() {返回选择;};返回 {放置集:放置集,获取集:获取集};});

我可以从我这样定义的函数中访问 selected:

setDisplay = function($scope, $mdDialog, shareData) {console.log(shareData.getSet());//这是有效的$scope.selected = shareData.getSet();$scope.hide = function() {$mdDialog.hide();};$scope.cancel = function() {$mdDialog.cancel();};$scope.answer = 函数(答案){$mdDialog.hide(答案);};};

我的控制器是这样的:

app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',函数 ($scope, $http, $mdDialog, $mdToast, shareData) {console.log(shareData.getSet());//不工作}]);

解决方案

您的 topicController 控制器的工厂函数中有多余的 $mdToast,您需要将其删除.

它不工作的原因是,目前你在数组中提到了 4 个依赖项,如 ['$scope', '$http', '$mdDialog', 'shareData', function &那么您将在 DI 数组旁边的函数内使用它的实例.在该函数中,您实际上有 5 个依赖项,其中 $mdToast 是额外的.所以在幕后发生的事情是函数的 $scope 持有一个 '$scope' DI 数组的值,同样你从右到左.但是当涉及到 $mdToast(在控制器函数中)时,它持有的值是 'shareData'(DI 数组)&那么下一个参数 shareData 什么也得不到.

app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',function ($scope, $http, $mdDialog, shareData) {//<--removed $mdToastconsole.log(shareData.getSet());}]);

<块引用>

注意:您正在使用 DI 内联数组注释,所以在数组中注入依赖项的顺序,您应该按照相同的顺序然后注入底层工厂函数.

I am trying to share a variable between a controller and a function. But i get an error from the controller, saying this:

TypeError: Cannot read property 'getSet' of undefined 

I have gone through numerous tutorials, but don't know where am I going wrong. My service code is like this:

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

    var selected = ["plz", "print", "something"];

    var putSet = function(set) {
        selected = set;
    };

    var getSet = function() {
        return selected;
    };

    return {
        putSet: putSet,
        getSet: getSet
    };
});   

I am able to reach selected from my function defined like this:

setDisplay = function($scope, $mdDialog, shareData) {

    console.log(shareData.getSet()); // this is working

    $scope.selected = shareData.getSet();
    $scope.hide = function() {
        $mdDialog.hide();
    };
    $scope.cancel = function() {
        $mdDialog.cancel();
    };
    $scope.answer = function(answer) {
        $mdDialog.hide(answer);
    };
};

My controller is like this:

app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',
function ($scope, $http, $mdDialog, $mdToast, shareData) {

    console.log(shareData.getSet()); // NOT WORKING

}]); 

解决方案

You had extra $mdToast in your topicController controller's factory function, you need to remove it.

The reason behind it was not working is, currently you had 4 dependency mentioned in array like ['$scope', '$http', '$mdDialog', 'shareData', function & then you are using its instance inside the function next to DI array. Inside that function you had actually 5 dependencies where $mdToast extra. So behind the scene what happening is $scope of function hold an value of '$scope' DI array likewise you go right to left. But when it comes to $mdToast(in controller function) it was holding a value of 'shareData'(of DI array) & then the next parameter shareData get nothing.

app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData', 
      function ($scope, $http, $mdDialog, shareData) { //<--removed $mdToast
    console.log(shareData.getSet());
  }
]);

NOTE: You are using DI inline array annotation, so the sequence in which dependency are injected in array, in same sequence you should inject then in underlying factory function.

这篇关于Angular:无法使用服务访问控制器中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆