Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在执行函数后无法更改Angular JS Service.JS中的数据?_Javascript_Angularjs_Service - Fatal编程技术网

Javascript 为什么在执行函数后无法更改Angular JS Service.JS中的数据?

Javascript 为什么在执行函数后无法更改Angular JS Service.JS中的数据?,javascript,angularjs,service,Javascript,Angularjs,Service,我有这个服务,我用我的2个控制器在不同的html页面的每一个。当我单击该函数时,它无法更改我先前设置的默认变量 服务 angular.module('starter.services', []) .service('ContentSize', function() { var triggers = [{ count: 1 }]; var counters = [{ index: 0

我有这个服务,我用我的2个控制器在不同的html页面的每一个。当我单击该函数时,它无法更改我先前设置的默认变量

服务

angular.module('starter.services', [])
    .service('ContentSize', function() {
        var triggers = [{
            count: 1
        }];
        var counters = [{
            index: 0
        }, {
            index: 1
        }, {
            index: 2
        }];
        return {
            getCounters: function() {
                return counters;
            },
            getCounter: function() {
                for (var i = 0; i < counters.length; i++) {
                    if (counters[i].index = triggers[0].count) {
                        return [counters[i].index];
                        break;
                    }
                }
            },
            setIndex: function(counter) {
                triggers[0].count = counter;
                console.log(triggers[0].count);
            },
            getIndex: function() {
                return triggers[0].count;
            }
        };
    });

如果将console.log“放在”服务的返回部分之外,那么它将始终只被调用一次:当服务首次初始化时。这是因为您放在返回之外的代码只在服务实例化时执行,这只在Angular服务是单例时发生

从服务返回的函数,即getCounters、getCounter、setIndex和getIndex是要返回的服务对象的属性。这些有效地充当服务的API,并且是控制器可以自由调用的“公共”函数

因此,每当控制器对其中一个函数进行调用时,都会调用其中任何函数中的代码,因此每次调用该函数时都会执行其中一个函数中的console.log


但是此返回服务对象之外的代码只被调用一次,这就是为什么在返回调用之外声明的console.log也只被调用一次。

我们还可以看到调用服务方法的代码。还要检查是否出现任何控制台错误。要将代码放入问题中,只需粘贴代码,选择代码并按“Ctrl+K”@Yasser我已经添加了控制器,对于错误,在我使用它开发我的Web时,浏览器中没有看到任何错误。因此,您似乎正在从ModalController调用服务的setIndex方法,请检查您传递的值x。控件是否已到达服务?@Yasser该值已传递,日志显示了我使用的x值thx@Untangled,现在我了解更多。
//This is the 1st controller :
.controller('cat1Controller' , function($scope, ContentSize) {

    //Text Size Declaration for text in page
    $scope.textTriggers = [{count: 0, size:2}];

    $scope.Index = ContentSize.getIndex();
    if ($scope.Index == 0) {
        $scope.textTriggers = [{count: 0, size:2}];
        console.log('Default');
    } 
    else if ($scope.Index == 1) {
        $scope.textTriggers = [{count: 1, size:3}];
        console.log('Medium');
    }
    else {
        $scope.textTriggers = [{count: 2, size:4}];
        console.log('Large');
    }
})

//The2nd Contorller
.controller('ModalController', function($scope, $ionicModal, ContentSize) {
  $ionicModal.fromTemplateUrl('templates/settingmodal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function() {
    $scope.modal.show();
  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  $scope.small = function (x) {
    ContentSize.setIndex(x);
  };
  $scope.medium = function (x) {
      ContentSize.setIndex(x);
  };
  $scope.large = function (x) {
    ContentSize.setIndex(x);
  };
});