2017-08-29 2 views
1

AngularJSのng-repeat(work in workList)から動的データを取得する単一のモーダルを作成しようとしています。つまり、動的コンテンツを1つのモーダルに追加するだけで複数のモーダルが生成されないように、ng-repeat内にはモーダルが存在しません。ここに私の見解です:ここではng-repeat内部からの単一モーダルデータの引き出し

<div ng-controller="mainController"> 
    <div ng-repeat="work in workList" 
    //some other code here... 
    <div ng-controller="modalController"> 
     <button ng-click="open_modal(work)" data-toggle="modal" data-target="#myModal"> 
    </div> 
</div> 
<!-- Modal --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <h2> 
    {{ timechosen.data.name }} 
    </h2> 
</div> 
</div> 

は私modalControllerです:

私のモーダルで
angular 
    .module('testApp') 
    .controller('modalController', ['$scope', function($scope) { 
    $scope.open_modal = function(time_chosen) { 
     $scope.timechosen = time_chosen.data; 
    } 
    }]) 

{{ timechosen.data.name }}は何も表示されないと私はそれは私が私のmainController$scope.timechosenを設定する際ためのネストスコープの問題ですかなり確信しています、それは正常に動作するようです。しかし、私は$scope.timechosenをmodalControllerに設定する必要がありますが、可能な入れ子スコープの問題を回避する方法が見つからないようです。何か案は?

答えて

0

あなたはすべてを1台のコントローラの中に置くことを考えましたか?このように:あなたが複数のコントローラに侵入する必要がある場合

angular 
 
    .module('app', []) 
 
    .controller('TestController', function($scope) { 
 
    $scope.items = [ 
 
     { 
 
     label: 'Item 1', 
 
     body: 'This is the body of item 1', 
 
     title: 'This is number 1' 
 
     }, 
 
     { 
 
     label: 'Item 2', 
 
     body: 'This is the body of item 2', 
 
     title: 'This is number 2' 
 
     }, 
 
     { 
 
     label: 'Item 3', 
 
     body: 'This is the body of item 3', 
 
     title: 'This is number 3' 
 
     } 
 
    ] 
 
    
 
    $scope.changeModalInfo = function(info) { 
 
     $scope.info = info; 
 
    } 
 
    });
@import "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css";
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Test</title> 
 
    <script 
 
    src="https://code.jquery.com/jquery-3.2.1.min.js" 
 
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
 
    crossorigin="anonymous"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body ng-app="app"> 
 

 
    <div ng-controller="TestController as ctrl"> 
 
    <ul> 
 
     <li ng-repeat="item in items"> 
 
     <button data-toggle="modal" data-target="#myModal" 
 
     ng-click="changeModalInfo(item)"> 
 
     {{item.label}} 
 
    </button> 
 
     </li> 
 
    </ul> 
 
    
 
    <div class="modal fade" 
 
     id="myModal" 
 
     tabindex="-1" 
 
     role="dialog" 
 
     aria-labelledby="myModalLabel"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title">{{info.title}}</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     <p>{{info.body}}</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
    </div><!-- /.modal-content --> 
 
    </div> 
 
    </div> 
 
    </div> 
 

 
</body> 
 
</html>

、あなたはサービスや$ rootScopeを(推奨されません)を使用することができます。

angular 
 
    .module('app', []) 
 
    .controller('ModalController', function($scope, ModalService) { 
 
    $scope.modal = ModalService; 
 
    }) 
 
    .controller('TestController', function($scope, ModalService) { 
 
    $scope.items = [ 
 
     { 
 
     label: 'Item 1', 
 
     body: 'This is the body of item 1', 
 
     title: 'This is number 1' 
 
     }, 
 
     { 
 
     label: 'Item 2', 
 
     body: 'This is the body of item 2', 
 
     title: 'This is number 2' 
 
     }, 
 
     { 
 
     label: 'Item 3', 
 
     body: 'This is the body of item 3', 
 
     title: 'This is number 3' 
 
     } 
 
    ] 
 
    
 
    $scope.changeModalInfo = function(info) { 
 
     ModalService.info = info; 
 
    } 
 
    }) 
 
    .service('ModalService', function() { 
 
    return {}; 
 
    });
@import "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css";
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Test</title> 
 
    <script 
 
    src="https://code.jquery.com/jquery-3.2.1.min.js" 
 
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
 
    crossorigin="anonymous"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body ng-app="app"> 
 

 
    <div ng-controller="TestController as ctrl"> 
 
    <ul> 
 
     <li ng-repeat="item in items"> 
 
     <button data-toggle="modal" data-target="#myModal" 
 
     ng-click="changeModalInfo(item)"> 
 
     {{item.label}} 
 
    </button> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
    
 
    <div class="modal fade" 
 
     id="myModal" 
 
     tabindex="-1" 
 
     role="dialog" 
 
     aria-labelledby="myModalLabel" 
 
     ng-controller="ModalController as ctrl"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title">{{modal.info.title}}</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     <p>{{modal.info.body}}</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
    </div><!-- /.modal-content --> 
 
    </div> 
 
    </div> 
 

 
</body> 
 
</html>

関連する問題