【问题标题】:Angular modal won't close (making $scopes talk)Angular modal 不会关闭(让 $scopes 说话)
【发布时间】:2015-04-20 14:55:23
【问题描述】:

我有一个简单的角度模态,它是由 angular ui.bootstrap 引起的。它打开得很好,传递值等,但不会关闭或取消。我认为这是 $scopes 讨论问题。我知道每个模态都有自己的 $scope,我只是不确定如何让它们相互交谈。

这是模态的标记:

<div>{{entry}}</div>
<button class="btn btn-primary">{{ $modalInstance.close() }}Close</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>

两种方法都试了,但都不管用...

这是“打开”模式的控制器:

.controller('dashboard',['$scope', '$rootScope', '$location', '$modal', 'loginService', function($scope, $rootScope, $location, $modal, loginService){

          var allowed = loginService.authenticate();

          allowed.get(
              function(result){
                  console.log(result);
                  if (result.loggedin !== undefined && result.loggedin === true) {
                      console.log("Welcome!");
                  }
              },
              function(result) {
                  $location.path("admin");
                  $rootScope.errorVisible = true;
              }
          );

          $scope.open = function () {

              var modalInstance = $modal.open({
                  templateUrl: '/partials/submission_mod.html',
                  controller: ['$scope', '$modalInstance', function($scope, $modalInstance){
                      $scope.modalInstance = $modalInstance;
                      $scope.entry = "Submission info goes here.";
                  }]
              });
          };
    }])

现在.. $scope.close 会进入仪表板控制器还是仍然尝试坐在自己的控制器中,如 here 所示?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    关闭功能需要在控制器中。

    $scope.open = function () {
    
              var modalInstance = $modal.open({
                  templateUrl: '/partials/submission_mod.html',
                  controller: ['$scope', '$modalInstance', function($scope, $modalInstance){      
                      $scope.close = function () {
                           $modalInstance.dismiss('cancel');
                      };    
    
                      $scope.modalInstance = $modalInstance;
                      $scope.entry = "Submission info goes here.";
                  }]
              });
          };
    

    【讨论】:

      【解决方案2】:
      <button class="btn btn-primary">{{ $modalInstance.close() }}Close</button>
      

      应该是

      <button class="btn btn-primary" ng-click="modalInstance.close()">Close</button>
      

      您必须将点击处理程序绑定到关闭按钮

      【讨论】:

      • 刚刚发现:p 谢谢!
      猜你喜欢
      • 2018-01-06
      • 1970-01-01
      • 2013-08-30
      • 2017-11-28
      • 2015-06-12
      • 2019-03-29
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多