1

我想让我的 Bootstrap 模态与 AngularJS 一起工作。一种模式是

    <div id="addToPlaylist" class="modal hide fade" aria-hidden="true" aria-labelledby="addToPlaylistLabel" role="dialog">
        <div class="modal-header">
                <h3>My Modal</h3>
            </div>
            <div class="modal-body">
MODAL BODY
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>

它将歌曲添加到用户已有的播放列表或创建新的播放列表。我想将它与一组歌曲一起使用,但一次只能与一首歌曲一起使用(目前)。

<div class="cell" ng-repeat="song in media.data">
</div> <!-- grid-wrapper -->

角控制器是

'use strict';

angular.module('PortalApp')
  .controller('SongsCtrl', function ($scope, MediaService, $location, AudioPlayerAPI, FavoriteService) {

$scope.toggleFaves = function (song) {

  FavoriteService.toggle(song).success(function (response) {
    if (response.successFlag) {
      song.favorite = !song.favorite;        
    }
  });
}    

$scope.fetched = false;

});

我该如何进行这项工作?

PS 我不想要 jQuery,只想要 Angular 的 jQuery Lite 插件。

4

2 回答 2

3

您应该查看 AngularUI Bootstrap modals ( http://angular-ui.github.io/bootstrap/#/modal )。传入和传出数据非常容易,并且都以 Angular 方式工作。

于 2013-09-18T20:46:07.240 回答
0

这是一个可重用的 Angular 指令,它将隐藏和显示 Bootstrap 模式。

app.directive("modalShow", function () {
    return {
        restrict: "A",
        scope: {
            modalVisible: "="
        },
        link: function (scope, element, attrs) {

            //Hide or show the modal
            scope.showModal = function (visible) {
                if (visible)
                {
                    element.modal("show");
                }
                else
                {
                    element.modal("hide");
                }
            }

            //Check to see if the modal-visible attribute exists
            if (!attrs.modalVisible)
            {

                //The attribute isn't defined, show the modal by default
                scope.showModal(true);

            }
            else
            {

                //Watch for changes to the modal-visible attribute
                scope.$watch("modalVisible", function (newValue, oldValue) {
                    scope.showModal(newValue);
                });

                //Update the visible value when the dialog is closed through UI actions (Ok, cancel, etc.)
                element.bind("hide.bs.modal", function () {
                    scope.modalVisible = false;
                    if (!scope.$$phase && !scope.$root.$$phase)
                        scope.$apply();
                });

            }

        }
    };

});

使用示例 #1 - 假设您要显示模式 - 您可以添加 ng-if 作为条件

<div modal-show class="modal fade"> ...bootstrap modal... </div>

Usage Example #2 - this uses an Angular expression in the modal-visible attribute

<div modal-show modal-visible="showDialog" class="modal fade"> ...bootstrap modal... </div>

Another Example - to demo the controller interaction, you could add something like this to your controller and it will show the modal after 2 seconds and then hide it after 5 seconds.

$scope.showDialog = false;
$timeout(function () { $scope.showDialog = true; }, 2000)
$timeout(function () { $scope.showDialog = false; }, 5000)

I'm late to contribute to this question - created this directive for another question here. Simple Angular Directive for Bootstrap Modal

Hope this helps.

于 2013-10-29T21:12:39.797 回答