1

我正在使用角度基础,特别是模态http://madmimi.github.io/angular-foundation/#/modal,我对如何在使用一个控制器时将数据传递给模态感到困惑,我想要一个数组值并更新模态以显示特定的用户信息,例如: $scope.updateUserInfo = $scope.user[index] ,唯一的问题是如何将数据传递给模态。

myApp.controller('users',function ($scope,$location,$http,$modal,msg) {
$http.get('api/v1/users')
.success(function (data,status) {
    $scope.user = data;
})
.error(function (data,status) {
    $location.path('/login');
});

$scope.showWrite = function () {
    $scope.write = true;
}

$scope.closeWrite = function () {
    $scope.write = false;
    $scope.newUser = '';
}

$scope.save = function () {
    $http.post('api/v1/users/store',$scope.newUser)
    .success(function (data,status) {

        $scope.user.unshift({
            id: data,
            first_name: $scope.newUser.first_name,
            last_name: $scope.newUser.last_name,
            email: $scope.newUser.email,
            role: $scope.newUser.role
        });

        $scope.write = false;
        $scope.newUser = '';
    })
    .error(function (data,status) {
        alert('failed');
    });
}

$scope.confirmDelete = function (index,id) {
    msg.confirmDelete().then(function(value) {
        $scope.text = msg.getText();
        $http.get('api/v1/users/destroy/'+id)
        .success(function  (data,status) {
            $scope.user.splice(index,1);
        })
        .error(function (data,status) {
            alert('Error : Operation failed');
        });
    });
}

$scope.showUserInfo = function () {

}

$scope.userUpdate = function () {

}


$scope.showUserUpdate = function (index) {
    $modal.open({
        templateUrl: 'partials/message/update.html',
        controller: 'users'
    });
}

});

4

2 回答 2

3

要将数据传递给 $modal 您需要更新 $modal 函数,如下所示:

$scope.showUserUpdate = function (popUpData) {
    var modalInstance = $modal.open({
        templateUrl: 'partials/message/update.html',
        controller: ['$scope', '$rootScope', '$modalInstance', 
        function($scope, $rootScope, $modalInstance) {
            $scope = angular.extend($scope, popUpData);
        }],
        resolve: {}
    });
    return modalInstance;
};

所以 popupData 是你想要传递给你的模式的数据。popupdata 然后将与该控制器的现有范围合并。现在您可以访问 HTML 中的 popupData 键。请记住,我们在此函数中返回模态实例,因此您可以使用此实例手动关闭弹出窗口。

于 2014-04-22T08:15:18.550 回答
0

另一种方法是使用 resolve 属性并将其注入控制器:

$scope.showUserUpdate = function (popUpData) {
    var modalInstance = $modal.open({
        templateUrl: 'partials/message/update.html',
        controller: ['$modalInstance', 'data', function($modalInstance, data) {
            data.popUpData = ...
        }],
        resolve: {
            data: popUpData
        }
    });
    return modalInstance;
};
于 2016-02-08T11:30:21.290 回答