简体   繁体   English

Modal上的控制器在AngularJS上不起作用

[英]Controller on Modal not working on AngularJS

I have a main users page with users, if you select a user, a modal is opened and should show the user information. 我有一个包含用户的主用户页面,如果您选择一个用户,则会打开一个模式并显示用户信息。

The problem is that the $scope doesn't seem to be working, as it doesn't show the user data on the modal. 问题在于$ scope似乎不起作用,因为它没有在模式上显示用户数据。 But if I show that user data anywhere on the main users page, it works fine. 但是,如果我在主用户​​页面的任何位置显示该用户数据,它就可以正常工作。

I have this controller: 我有这个控制器:

function userController($scope, $modal, $http, $rootScope) {
    var usrCtrl = this;
    $scope.users = null;
    $scope.openUserForm = function () {
        var modalInstance = $modal.open({
            templateUrl: 'views/modal_user_form.html',
            controller: userController,
            windowClass: "animated flipInY"
        });
    };

var session = JSON.parse(localStorage.session);
$http({
method: 'GET',
url: $rootScope.apiURL+'getAllClientUsers/'+session,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response){
    if(response.ErrorMessage === null && response.Result !== null){
        $scope.users = response.Result; 
    }
});

//**This is the code that opens the modal
$scope.editViewUser = function(user){
    for (var key in $scope.users) {
        if($scope.users[key].SecUserId === user){
            $scope.selectedUser = $scope.users[key];
        }
    }
    $scope.openUserForm();
};

}; };

And this modal: 这个模态:

    <div ng-controller="userController">
<div class="inmodal">
    <div class="modal-header">
        <h4 class="modal-title">Create or Edit User</h4>
        <small class="font-bold">Use this modal to create or edit a user</small>
    </div>
    <div class="modal-body">

        <form method="get" class="form-horizontal">
                        <div class="form-group"><label class="col-sm-2 control-label">Nombre</label>
                            <div class="col-sm-10"><input ng-model="selectedUser.FirstName" type="text" class="form-control"></div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Apellido</label>
                            <div class="col-sm-10"><input ng-model="selectedUser.LastName" type="text" class="form-control"></div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Usuario</label>
                            <div class="col-sm-10"><input ng-model="selectedUser.UserName" type="text" class="form-control"></div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Email</label>
                            <div class="col-sm-10"><input ng-model="selectedUser.Email" type="email" class="form-control"></div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Activo</label>
                            <div class="col-sm-10">
                                <div><label> <input ng-model="selectedUser.FirstName" type="checkbox" value=""></label></div>
                            </div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Email Verificado</label>
                            <div class="col-sm-10">
                                <div><label> <input ng-model="selectedUser.FirstName" type="checkbox" value=""></label></div>
                            </div>
                        </div>
                        <div class="form-group"><label class="col-sm-2 control-label">Privilegios</label>
                            <div class="col-sm-10">
                                <select class="form-control m-b" name="account">
                                    <option ng-repeat="priv in selectedUser.EffectivePrivileges">{{ priv }}</option>
                                </select>
                            </div>
                        </div>
                    </form>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-white" ng-click="cancel()">Close</button>
        <button type="button" class="btn btn-primary" ng-click="ok()">Save changes</button>
    </div>
</div>
</div>

I tried injecting the controller and also using the ng-controller, and scope property for modal. 我尝试注入控制器,也使用ng-controller和modal属性作为模态。

What am I missing? 我想念什么?

The problem is that modal template is compiled in the scope which doesn't inherit from your $scope . 问题在于模式模板是在不继承自$scope的范围内编译的。 To make a connection between them you can tell modal to create a new child scope of your scope: 要在它们之间建立联系,您可以告诉modal创建您的作用域的新子作用域:

$scope.openUserForm = function () {
    var modalInstance = $modal.open({
        scope: $scope, // <----- add this instruction
        templateUrl: 'views/modal_user_form.html',
        controller: userController,
        windowClass: "animated flipInY"
    });
};

Generally, the modal would have its own controller rather than point to the controller that its called from. 通常,模态将具有其自己的控制器,而不是指向其调用的控制器。 You would inject the model (selectedUser) into the modal controller, work on it, then pass it back to parent. 您可以将模型(selectedUser)注入到模态控制器中,对其进行处理,然后将其传递回父级。 Also, you can give user option to cancel in which case no changes are made to parent controller model. 此外,您可以给用户选项以取消,在这种情况下,无需更改父控制器模型。

Modal controller: 模态控制器:

app.controller('UserModalController', [
    '$scope',
    '$modalInstance',
    'selectedUser',
    function ($scope, $modalInstance, selectedUser) {

    $scope.selectedUser = selectedUser;

    $scope.cancel= function () {
        // user cancelled, return to parent controller
        $modalInstance.dismiss('cancel');
    };

    $scope.save = function () {
        // close modal, return model to parent controller
        $modalInstance.close($scope.selectedUser);
    };

]);

Changes to main controller: 对主控制器的更改:

                var modalInstance = $modal.open({
                  templateUrl: 'views/modal_user_form.html',
                  controller: 'UserModalController',
                  windowClass: "animated flipInY",
                  resolve: {
                    selectedUser: function() {
                      return $scope.selectedUser;
                    }
                  }
                });

                modalInstance.result.then(function(updatedUser) {
                  // deal with updated user
                }, function() {
                  // modal cancelled
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM