Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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 在服务angularjs中的函数中使用$scope_Javascript_Angularjs - Fatal编程技术网

Javascript 在服务angularjs中的函数中使用$scope

Javascript 在服务angularjs中的函数中使用$scope,javascript,angularjs,Javascript,Angularjs,我知道同样的问题已经被问了很多次,但我找不到符合我要求的确切问题。所以我的问题是: 我的控制器中有一个功能: showPNG = function() { var pngPromise = $modal({ template: 'index.html', persist: true, show: false, backdrop: 'static', scope:

我知道同样的问题已经被问了很多次,但我找不到符合我要求的确切问题。所以我的问题是:

我的控制器中有一个功能:

showPNG = function() {
        var pngPromise = $modal({
            template: 'index.html',
            persist: true,
            show: false,
            backdrop: 'static',
            scope: $scope,
            modalClass: 'preview-png'
        });
        $q.when(pngPromise).then(
            function(pngElement) {
                pngElement.modal('show');
            }
        );
};
我有3个控制器具有相同的功能。因此,我试图以这样一种方式重构它,即它是在某个服务中编写的,并且可以从所有控制器调用它。到目前为止,我所做的是:

在用:

var service = module.exports =   function ($modal, $q) {
        return {
            showPNG : function(scope) {
                var pngPromise = $modal({
                    template: 'index.html',
                    persist: true,
                    show: false,
                    backdrop: 'static',
                    scope: scope,
                    modalClass: 'preview-png'
                });
                $q.when(pngPromise).then(
                    function(pngElement) {
                        pngElement.modal('show');
                    }
                );
            }
        };
};
service.$inject = ['$modal', '$q']; 
在控制器中:

...
    myService.showPNG($scope);
...
这段代码没有任何错误。但问题是,将
$scope
作为参数传递给函数is service是否会产生任何副作用?有没有更好的替代方法


谢谢。

我建议不要将$scope传递给该服务。这使您的服务依赖于使用它的控制器。如果另一个控制器需要使用该服务怎么办

相反,让服务返回承诺,并在控制器中执行以下操作:

.controller("ctrl1", ["$scope", "service", function($scope, service){
   $scope.showModal = false;
   service.showPNG()
      .then(function(shouldShow){
         $scope.showModal = shouldShow;
         $scope.$apply(); // assuming showPNG is async
      });
}]);
编辑: 下面是如何实现模式对话服务的示例: