提交模式时如何调用范围?

时间:2016-03-21 10:33:30

标签: angularjs angular-ui-bootstrap

我在我的项目中使用angular-ui-bootstrap并使用bootstrap模式。但是当我提交时我的模态有问题我无法调用范围而我收到错误" $ scope.testAlert不是函数&# 34;

控制器:

$scope.open = function () {
        $uibModal.open({
            animation: $scope.animationsEnabled,
            templateUrl: 'userModal.html',
            controller: function ($scope, $uibModalInstance, user) {
                $scope.user = user;

                $scope.save = function () {

                    var editUser = userService.updateUser($scope.user);
                    editUser.then(function () {
                        getAllUsers();
                        $scope.testAlert();
                    })

                    $uibModalInstance.close();
                }
            },
            resolve: {
                user: function () {
                    return $scope.user;
                }
            }
        });
    }

$scope.testAlert = function () {
        alert("Blah blah");
    }

1 个答案:

答案 0 :(得分:1)

$ scope.testAlert位于主控制器的$ scope上,而不是模态控制器。你需要像这样移动它:

$scope.open = function() {
  $uibModal.open({
    animation: $scope.animationsEnabled,
    templateUrl: 'userModal.html',
    controller: function($scope, $uibModalInstance, user) {
      $scope.user = user;

      $scope.save = function() {

        var editUser = userService.updateUser($scope.user);
        editUser.then(function() {
          getAllUsers();
          $scope.testAlert();
        })

        $uibModalInstance.close();

        $scope.testAlert = function() {
          alert("Blah blah");
        }
      }
    },
    resolve: {
      user: function() {
        return $scope.user;
      }
    }
  });

}