简体   繁体   English

如何使用角度用户界面$ modal.popup()绑定范围/模型

[英]how to bind scope/model with angular ui $modal.popup()

I have a ng-repeat list view with each item has edit button. 我有一个ng-repeat列表视图,每个项目都有编辑按钮。 I want to open a modal popup and populate values associated with that list item into the popup fields. 我想打开一个模式弹出窗口,并将与该列表项关联的值填充到弹出字段中。

I am loading the popup the usual way with $scope.modalInstance = $modal.open({...}); 我使用$scope.modalInstance = $modal.open({...});以通常的方式加载弹出窗口$scope.modalInstance = $modal.open({...});

What I am struggling with is, how do I bind the scope of the popup! 我正在努力的是,如何绑定弹出窗口的范围! I know scope can be passed as a parameter while initializing it. 我知道范围可以在初始化时作为参数传递。 But I want the popup to directly bind the fields with that specific list item. 但是我希望弹出窗口将字段与该特定列表项直接绑定。

I tried passing the list item like 我试图像这样传递列表项

var item = $scope.list[index];

$scope.modalInstance = $modal.open({
    ...,
    scope: item
});

But it gives error as TypeError: $rootScope.$new is not a function . 但是它给出了TypeError: $rootScope.$new is not a function错误TypeError: $rootScope.$new is not a function May be this isn't the correct way I should bind the list item. 可能这不是我应该绑定列表项的正确方法。 Please guide in right direction. 请引导正确的方向。

Adding the full method which triggers editing of the list item 添加触发列表项编辑的完整方法

$scope.editItem = function (index) {
    $scope.item = $scope.api.select[index];

    $scope.modalInstance = $modal.open({
        templateUrl: 'templates/pupup-edit-item.html',
        scope: $scope,
    });

    $scope.modalInstance.result.then(function () {
        // ...
    }, function () {
        // $log.info('Modal dismissed at: ' + new Date());
    });
}

And the template, 还有模板

<div class="modal-header">
        <h3 class="modal-title">Edit Item</h3>
</div>
<div class="modal-body">
    <input ng-model="item.name" />
</div>
<div class="modal-footer">
        <button class="btn btn-primary" ng-click="popup_Ok()">OK</button>
        <button class="btn btn-default" ng-click="popup_Cancel()">Cancel</button>
</div>

I suppose you have a separate template with the modal window. 我想您有一个带有模态窗口的单独模板。 Inside that template you define a local scope which is going to get populate by filtering the list of values from the controller. 在该模板内,您定义了一个本地范围,该范围将通过过滤控制器中的值列表来填充。

So the modal content should look like this: 因此,模态内容应如下所示:

<div modal="isProductShowing" close="hideFullProduct()" options="productModalOptions">    
    <section class="container modal-body product-modal">    
        <button class="modal-body__close" ng-click="hideFullProduct()">Close</button>    

        <div class="product-modal__content">                
            <h1 class="product-modal__name">{{ currentFullProduct.name }}</h1>                            
        </div>                        
    </section>    
</div>

Then inside the controller you search for the product which is defined in the main template where you populate the list and associate the object to the scope of the modal window. 然后,在控制器内部搜索在主​​模板中定义的产品,并在其中填充列表并将对象与模式窗口的范围相关联。 So you need to filter the product which has the id defined as parameter in the listing template. 因此,您需要过滤清单模板中ID为参数定义的产品。

$scope.isProductShowing = false;

$scope.currentFullProduct = null;

$scope.productModalOptions = {
    backdropFade: true,
    dialogFade:true
};

$scope.showFullProduct = function (productId) {
    $scope.isProductShowing = true;
    $scope.currentFullProduct = _.findWhere($scope.products, { id: productId});
}

Only one thing remained: to trigger the modal window on click. 剩下的只有一件事:在单击时触发模式窗口。 This needs to be done on the main listing template: 这需要在主清单模板上完成:

<li ng-click="showFullProduct(product.productId)">

Hope it's clear, otherwise i can assist. 希望很清楚,否则我可以提供帮助。

yes you can bind like this, using resolve 是的,您可以使用resolve这样绑定

    var modalInstance = $modal.open({

       ......
       .... 
        resolve: {
            item: function() {
                return $scope.item;
            }
        }
    });

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

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