我正在创建一个Webapp,我想实现一个添加好友的选项。我已将添加好友页面创建为带有文本输入字段的模式。我想通过在视图页面上显示输入来进行测试。如何在我的视图页面上显示这些数据?
这是我目前拥有的
index.html
<div ng-controller="ModalDemoCtrl"> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h3 class="modal-title">I'm a modal!</h3> </div> <form name = "addFriendForm"> <input ng-model = "user.name"class="form-control" type = "text" placeholder="Username" title=" Username" /> {{ user.name }} </form> <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()">Add Friend</button> <div> Username: {{user.name}}</div> </div>
我的JavaScript文件:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']); angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) { $scope.user = {name: ""} $scope.open = function () { var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: 'ModalInstanceCtrl', resolve: { items: function () { return $scope.items; } } }); modalInstance.result.then(function () { $scope.user.name = user.name;}, function () { $log.info('Modal dismissed at: ' + new Date()); }); }; }); angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance) { $scope.ok = function () { $modalInstance.close($scope.user.name); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; });
plunkr:http ://plnkr.co/edit/JIoiNx47KXsY8aqbTUDS?p=preview
您可以利用modalInstance的resolve属性;这充当模式实例与父控制器之间的链接。
modalInstance
resolve
您将对象插入ModalInstanceController,并将其分配给模态实例的范围。
ModalInstanceController
UI Bootstrap解决的工作原理与ngRouter完全相同;因此,如果由于某种原因resolve无法 解析 对象,则不会打开模式。
var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: 'ModalInstanceCtrl', resolve: { user: function() { return $scope.user; } } });
另一种可以说是较简单的方法是将父级范围传递给模式。请注意,当前controllerAs在父控制器上使用语法时,这不起作用。
controllerAs
var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: 'ModalInstanceCtrl', scope: $scope });