bootstrap modal,angularjs ng-click获取按钮值

时间:2015-04-20 10:33:22

标签: jquery angularjs bootstrap-modal

我有一个打开带有列表(层次结构)的bootstrap模式的按钮我想要具有Selected list li值并显示为放在$scope中的按钮值 这里是链接jsfiddle以了解我的需要。 jsfiddle

1 个答案:

答案 0 :(得分:1)

您可以使用ui-bootstrap库,特别是 modal 指令来实现您的目标。

你不需要像在小提琴中那样使用jQuery。

修改:以下是 plunk 作为您需要做的示例。

的JavaScript

angular
.module('modalDemo', ['ui.bootstrap'])
.controller('DemoCtrl', function ($scope, $modal) {

    console.log("in angular");

    $scope.selected = { name: 'none' };

    $scope.open = function() {

        var modalInstance = $modal.open({
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          size: 'lg'
        });

        modalInstance.result.then(function (selectedItem) {
          $scope.selected = selectedItem;
        });
    };
})
.controller('ModalInstanceCtrl', function ($scope, $modalInstance) {

    $scope.setSelectedSegment = function (value) {

        $scope.selected = value;
        $modalInstance.close($scope.selected);
    };

    $scope.categoryList = [
        { name: 'Catégorie 1' },
        { 
            name: 'Catégorie 2',
            segments: [
                { 
                    name: 'Segment 1',
                    values: [
                        { name: 'Valuer 1' },
                        { name: 'Valuer 2' }
                    ]
                },
                { name: 'Segment 2' }
            ]
        },
        {
            name: 'Catégorie 3',
            segments: [
                { name: 'Segment 1' },
                { name: 'Segment 2' }
            ]
        },
        { name: 'Catégorie 4' },
        { name: 'Catégorie 5' }
    ];
});

HTML

<div class="container" ng-app="modalDemo">
  <div ng-controller="DemoCtrl" class="container">
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title">Sélection de la segmentation</h3>
        </div>
        <div class="modal-body">
            <div class="dd" id="nestable">
                <ol class="dd-list">
                    <li class="dd-item" ng-repeat="category in categoryList">

                        <div class="dd-handle">{{ category.name }}</div>

                        <ol class="dd-list" ng-if="category.segments">
                            <li class="dd-item" ng-repeat="segment in category.segments">

                                <div class="dd-handle">{{ segment.name }}</div>

                                <ol class="dd-list" ng-if="segment.values">
                                    <li class="dd-item" ng-repeat="value in segment.values">
                                        <div class="dd-handle" value="valeur1" ng-click="setSelectedSegment(value)">{{ value.name }}</div>
                                    </li>
                                </ol>
                            </li>
                        </ol>
                    </li>

            Selected: <b>{{ selected.name }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>
    <button class="btn btn-default" ng-click="open()">{{ selected ? selected.name : 'Segmentation' }}</button>
  </div>
</div>