【问题标题】:AngularJS: passing a model into and out of a Bootstrap UI modalAngularJS:将模型传入和传出 Bootstrap UI 模式
【发布时间】:2015-08-28 13:42:26
【问题描述】:

我的 Bootstrap UI 模式有问题。

我有一个从用户那里获取地址详细信息的模式,但由于某种原因,该模式总是空的。我的模板摘录:

<form class="form-horizontal" style="padding: 64px;">
    <div class="form-group">
        <label for="firstName" class="col-sm-3 control-label">First name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="firstName" ng-model="model.firstName">
        </div>
    </div>
    <div class="form-group">
        <label for="lastName" class="col-sm-3 control-label">Last name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="lastName" ng-model="model.lastName">
        </div>
    </div>
    <!-- etc -->
</form>

还有我的控制器:

module Test.Controllers {
    "use strict";

    export class ChooseAddressController {
        static $inject = ["$modal", "AddressService"];

        private addressService: Services.AddressService;

        public addresses: Array<Models.Address>;
        public modal;

        constructor($modal, addressService: Services.AddressService) {
            this.addressService = addressService;
            this.modal = $modal;
        }

        public newAddressClicked(): void {
            var newAddress = this.addressService.createNewAddress();
            this.editAddress(newAddress);
        }

        public editAddress(address: Models.Address): void {
            address.firstName = "Test 1";
            address.lastName = "Test 2";

            var modalInstance = this.modal.open({
                animation: true,
                templateUrl: 'dialogs/editAddressDialog.html',
                backdrop: "static",
                backdropClass: "windowModal",
                keyboard: false,
                resolve: {
                    model: () => address
                }
            });
        }
    }
}

我希望对话框包含“测试 1”和“测试 2”数据,但事实并非如此。我已经按照this post 的建议查看了this plnkr,但它似乎对我不起作用,这令人沮丧。

我做错了什么?

【问题讨论】:

    标签: angularjs twitter-bootstrap angular-ui-bootstrap


    【解决方案1】:

    嘿,这是我在控制器和模态之间传递数据并返回的方法:

    //Inject $modal into your Controller
    
    //Call the modal from your controller
    
    
    $scope.myModal = function() {
        var modalInstance = $modal.open({
            templateUrl: "some/ur/to/your/template.html",
            controller: "MyModalController",
            size: "lg",
            //Data which has to be passed into the MyModalController
            resolve: {
                data: function () {
                    return {                    
                        someData: $scope.someRandomDataWhichModalNeeds
                    };
                }
            }
        });
    
        //When $scope.confirm() is called from inside the modal do some shit with the new data!
        modalInstance.result.then(function (result) {
                console.log(result.modalData)
    
        });
    }
    
    //Handle data insde your modal  
    //data param will be your values you need pass in
    //If you want add some new data inside your modal just build upon the modalData object
    
    function MyModalController($scope, $modalInstance, data) {
    
        //assign the data you passed in to the modals scope so you can bind to it
        $scope.modalData = data;
        $scope.result = {
            modalData: $scope.modalData
        }
    
        //Dismiss() - call this in your modal template to dismiss the modal
        $scope.cancel = function () {
            $modalInstance.dismiss();
        }
    
        //Call this in your template so send all the modal data back to the controller
        $scope.confirm = function () {
            $modalInstance.close($scope.result);
        }
    }
    
    
    //In your template file you would bind your data:
    {{ modalData.someData }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多