【问题标题】:Angular How to pass scope to view from service?Angular如何将范围传递给服务视图?
【发布时间】:2014-06-30 11:55:27
【问题描述】:

我在辅助服务中有全局功能。

this.displayModalWithInput = function ($scope, title, cotroller, functionCallback) {
        $scope.modalProperties = {
            modalTitle : title,
            modalController : cotroller,
            modalFunction : functionCallback
        };

        $('#myModal').modal();
    };

在我的模板中,我有标准的 Bootsrap 模式窗口。问题是如果我尝试使用以下方式访问范围:

{{modalProperties.modalTitle}} 

什么都不显示。

如果我想使用全局可用函数并将数据从这个全局函数传递到可访问范围,该如何解决?

感谢您的任何建议。

【问题讨论】:

    标签: javascript angularjs templates view scope


    【解决方案1】:

    首先定义 $scope.modalProperties=[];在您的控制器中。 然后,当您从控制器调用 displayModalwithInput 函数时,请将 $scope 作为参数之一传递。例如displayModalwithInput($scope, ...) 如果它不起作用,我想看看你的全球服务代码。

    【讨论】:

      【解决方案2】:

      服务(工厂)是单例的,将范围注入服务并不是一个好的做法(我会说你不能)。


      在您的情况下,为了避免代码重复,我将创建指令(隔离范围),在 link 函数中您可以编写所有相关逻辑。

      此演示可能对您有所帮助PLUNKER

      指令示例

      .directive( 'confirmPopup', function () {
        return {
          restrict: 'A',
          replace: true,
          scope: { title: '@', content: '@', placement: '@', animation: '&', isOpen: '&', func: '&' },
          templateUrl: 'confirmPopup.html',
          link: function ($scope, element, attrs) {
            $scope.confirm = function(){
      
              $scope.$parent.deleteThing();
              $scope.$parent.tt_isOpen = false;
            }
            $scope.cancel = function(){
              $scope.$parent.tt_isOpen = false;
            }
          }
        };
      })
      

      【讨论】:

        猜你喜欢
        • 2013-03-08
        • 1970-01-01
        • 2013-07-31
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多