【问题标题】:ui-bootstrap Angular modal resolve and Scope valueui-bootstrap Angular 模态解析和范围值
【发布时间】:2016-04-02 07:09:53
【问题描述】:

我对 Angular 比较陌生,我已将 Bootstrap 模态集成到我的项目中,但我无法使用“程序控制器”中的值。

我想要 "ModalInstanceCtrl" 控制器内的 "programcontroller" 中的 id_program。我尝试将其添加到 RESOLVE 中,但无法获取数据。

我在模态的 "ModalInstanceCtrl" 控制器中使用 RESOLVE 从 "programcontroller" 成功获取了硬编码数据。

但由于我使用此片段获得了 id_prgram:

<input type="text" ng-hide=" true" ng-model="id_program" ng-init="id_program=item._id">

上面的代码在 ng-repeat 块内并被填充。我知道这是因为异步导致了问题。

我已附上代码供您参考。

catalogapp.controller('programcontroller', function ($scope, $uibModal, $log, $routeParams) {

$scope.id_program = "";

$scope.title_program = "";

$scope.filter_program = $routeParams.seasonId;

$scope.season_number = $routeParams.seasonNumber;

$scope.model = {
//got this from previous HTML page as routeparams
    season_id : $routeParams.seasonId
}
$scope.animationsEnabled = true;

$scope.program_array = [
    {
        "_id" : "program:a8097ae943bdbd372906ea494ddecbf2",
        "series" : "series:fcde9691e624ba50df9be71735f4bb14",
        "title" : "title1",
        "season" : "season:a6e4d728c316cdffa933490d4f538251",

    },
    {
        "_id" : "urn:esp:hbo:program:testprogram20160310",
        "title" : "title1",
        "series" : "series:4f19358c7377482f1310e5cfa06c5bd2",
        "season" : "season:a6e4d728c316cdffa933490d4f538251",
    }
]

$scope.items = [
    {
        "title" : "title",
        "_id" : "program:version:2541203f297f8f0d",
        "shortTitle" : "shtitle",
        "program" : "program:f555feb8dafc1bae42d",
    },
    {
        "title" : "title2",
        "_id" : "program:version:40a2313f297f8f0d",
        "shortTitle" : "shtitle2",
        "program" : "program:f55asfsa57927411bd6545feb8dafc1bae42d",
    },
    {
        "title" : "title3",
        "_id" : "program:version:403f297f8f0d",
        "shortTitle" : "shtitle3",
        "program" : "program:asdf5557927411bd6545feb8dafc1bae42d",
    }
]

$scope.open = function (size) {
    var modalInstance = $uibModal.open({
        animation: $scope.animationsEnabled,
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        size: size,
        backdrop: 'static',
        resolve: {
            items1: function () {
                return $scope.items;
            }
        }
    });

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

});


catalogapp.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items1) {
    $scope.items = items1;

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');


      };
});

HTML代码如下!!!!!!

<!doctype html>
<html>
<head>
    <title>HBO ESP Console</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-animate.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-route.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="/css/main.css">
    <script src="/controllers/programcontroller.js"></script>


</head>
<body style="background-color:#44484a">
    <h2>Program List</h2>

    <script type="text/ng-template" id="myModalContent.html">
        <body ng-controller="ModalInstanceCtrl">
            <div class="modal-header">
                <h3 class="modal-title">Program Version</h3>
            </div>
            <div class="modal-body" style="float:left">
                <ul ng-repeat="item in items" style="list-style-type:none">
                    <li class="image">
                        <button type="button" style="background-color: #555555" class="btn btn-default">
                            <img ng-src="http://icons.iconarchive.com/icons/atti12/tv-series-folder/512/Game-of-Thrones-icon.png" height="200" width="200" />
                        </button>
                    </li>
                </ul>
            </div>
            <div class="modal-footer">
                <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
            </div>
        </body>
    </script>

    <div ng-controller="programcontroller">
        <input ng-hide=" true" type="text" ng-model="filter_program"><br><br>
        <p ng-hide=" true">Season_id: {{model.season_id}}</p>
        <p style="font-size:18px">Season Number:{{season_number}}</p>
        <div style="float:left">
            <div style="float:left">
                <ul ng-repeat=" item in program_array | filter : filter_program" style=" list-style-type:none;float:left">
                    <li class="image">
                        <input type="text" ng-hide=" true" ng-model="id_program" ng-init="id_program=item._id">
                        <input type="text" ng-hide=" true" ng-model="title_program" ng-init="title_program=item.title">
                        <button type="button" style="background-color: #555555" class="btn btn-default" ng-click="open('lg')">
                            <img ng-src="http://icons.iconarchive.com/icons/atti12/tv-series-folder/512/Game-of-Thrones-icon.png" height="200" width="200" />
                        </button>
                        <p>{{item.title}}</p>
                        {{id_program}}
                    </li>
                </ul>
            </div>
        </div>
    </div>
</body>
</html>

请帮帮我!!!!!!!谢谢

【问题讨论】:

  • 您好,您可以使用您的代码添加指向 codepen 或 JSBin 的链接吗??

标签: javascript angularjs angular-ui-bootstrap bootstrap-modal


【解决方案1】:

您甚至在 Angular 处理过滤器值之前就尝试访问它。

这里是解决办法

<ul ng-repeat="it in itm">
<li>
  {{it._id}}
  <input type="text" ng-hide="true" >
  <button type="button" class="btn btn-default" ng-click="setFilter(it._id);open('lg')">Large modal --- {{it._id}}</button>
</li>
</ul>

控制器

$scope.setFilter = function (filterValue) {
   $scope.filter = filterValue;

}

这里是Plunkr

希望这会有所帮助!

【讨论】:

  • 还是不行!!!所有依赖项都是最新的,我想我不会被打扰,因为我仔细检查了......!!
  • 你能用你的代码创建一个 plunkr 或 JSBin 或 Codepen 吗??
  • 在上面的代码中,我需要 ModalInstanceCtrl 中的 来自 ModalDemoCtrl 的过滤器值....你能帮我解决这个问题吗
  • 您甚至在处理 Angular 之前尝试访问过滤器值并设置值。并且尽可能避免使用 JQuery 编程方式。这里的任何方式都是解决方案plnkr.co/edit/LB6jcNrJ5Yi8dYe4xTw跨度>
【解决方案2】:

中移除 ng-controller="ModalInstanceCtrl"
<script type="text/ng-template" id="myModalContent.html"> 

看看会发生什么。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多