微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 为什么如果从不同的表单调用按钮,Ng Repeat不起作用?

我有一个html表,其中包含一个ng重复指令和两个按钮.第一个将打开一个包含一个新表单的模态,并让我创建我的用户,然后当我点击保存它将添加到列表.第二个是以相同的原始形式添加一个用户.

我不明白为什么当我点击第一个按钮,这是一个不同的形式,我不能更新的ng重复,但第二个可能.
这是代码

homepage.jsp

<body ng-app="myApp">
    <div class="generic-container" ng-controller="UserController as ctrl">
        <div id="createuserContent.jsp" ng-include="createuserContent"></div>
        <table>
            <tr>
                <td>
                    <button type="button" class="btn btn-primary"
                    ng-click="ctrl.opencreateuser()">Create</button>
                </td>
            </tr>
        </table>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>ID.</th>
                    <th>Name</th>
                    <th>Address</th>
                    <th>Email</th>
                    <th width="20%"></th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="u in ctrl.users">
                    <td><span ng-bind="u.ssoId"></span></td>
                    <td><span ng-bind="u.firstName"></span></td>
                    <td><span ng-bind="u.lastName"></span></td>
                    <td><span ng-bind="u.email"></span></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

user_controller.js

'use strict';

App.controller('UserController',function ($scope,UserService,$window,$log,$uibModalStack,$uibModal,$rootScope) {
    var self = this;

    self.users = [];

    self.fetchAllUsers = function () {
        console.log('----------Start Printing users----------');
        for (var i = 0; i < self.users.length; i++) {
            console.log('FirstName ' + self.users[i].firstName);
        }
    };
        /**
    this function will not work
    **/
    self.saveUser = function (user) {
        self.users.push(user);
        self.fetchAllUsers();
        $log.log("saving user");
        $uibModalStack.dismissAll();
    };

    /**
    this function works fine
    **/
    self.addNewRow = function () {
        var specialUser = {
                id : 12,firstName : 'john',lastName: 'travolta',homeAddress : {location:'chicago'},email : 'trav@email.com'
        };
        self.users.push(specialUser);
        $log.log("saving specialUser");
    };
    self.opencreateuser = function () {

        var modalinstance = $uibModal.open({
                animation : true,templateUrl : 'createuserContent',controller : 'UserController',resolve : {
                    items : function () {
                        return $scope.items;
                    }
                }
            });

        modalinstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        },function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    self.fetchAllUsers();
});

createuserContent.jsp

<form role="form" ng-controller="UserController as ctrl" >
    <div class="form-group">
        <label for="FirstName">FirstName</label> <input type="FirstName"
            ng-model="ctrl.user.firstName" class="form-control"
            id="FirstName" placeholder="Enter FirstName" /> <label
            for="lastName">lastName</label> <input type="lastName"
            class="form-control" id="lastName"
            ng-model="ctrl.user.lastName" placeholder="Enter lastName" />
        <label for="email">Email address</label> <input type="email"
            ng-model="ctrl.user.email" class="form-control" id="email"
            placeholder="Enter email" />
    </div>
    <div class="form-group">
        <label for="homeAddressLocation">Home Address</label> <input class="form-control"
            ng-model="ctrl.user.homeAddress.location" id="homeAddressLocation"
            placeholder="homeAddressLocation" />
    </div>
    <div class="form-group">
        <label for="SSOId">SSOId</label> <input class="form-control"
            ng-model="ctrl.user.ssoId" id="SSOId" placeholder="SSOId" />
    </div>
    <button type="submit" class="btn btn-default"
        ng-click="ctrl.saveUser(ctrl.user)">Save</button>
    <button type="submit" class="btn btn-default">Cancel</button>
</form>

解决方法

由于您的模态模板无法访问您的UserController对象,并且不显示错误,因为您在模态模板相同的控制器中使用,因此重新加载为新的Ctrl不引用父Ctrl.

但是更好的是使用不同的控制器并将父控制器对象传递给模态控制器,然后模态主体可以使用所有父对象.所以你应该将父对象传递给模态控制器.

当您在主文件中包含createuserContent.jsp弹出文件时,不需要在modalinstance控制器中使用的模态模板中使用ng-controller =“UserController as ctrl”:“Ctrl”

喜欢:

var modalinstance = $uibModal.open({
      templateUrl: 'createuserContent.jsp',controller: 'ModalCtrl',// ModalCtrl for modal
      controllerAs:'modal',// as modal so no need to use in modal template
      size: 'lg',resolve: {
        items: function () {
          return $scope.items;
        },parent: function(){ // pass self object as a parent to 'ModalCtrl'
                return self;
        }
      }

和ModalCtrl像:

.controller('ModalCtrl',['parent',function (parent) {
    this.parent = parent;
}]);

这里使用ModalCtrl作为模态,以便您可以访问父对象,如:modal.parent.user

模板如下:

<form role="form" >
    <div class="form-group">
    <label for="FirstName">FirstName</label> <input type="FirstName"
ng-model="modal.parent.user.firstName" class="form-control"
id="FirstName" placeholder="Enter FirstName" />
        .....
        ....
    <button type="submit" class="btn btn-default"
ng-click="modal.parent.saveUser(modal.parent.user)">Save</button>
    <button type="submit" class="btn btn-default">Cancel</button>
    </form>

更多细节Visit PLUNKER DEMO

原文地址:https://www.jb51.cc/js/152649.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐