Man*_*tto 53 angularjs angular-ui angular-ui-bootstrap
plunker:http://plnkr.co/edit/wURNg8ByPYbEuQSL4xwg
example.js:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal) {
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'modal.html',
controller: 'ModalInstanceCtrl'
});
};
};
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.ok = function () {
alert($scope.text);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
Run Code Online (Sandbox Code Playgroud)
index.html的:
<!doctype html>
<html ng-app="plunker">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
<script src="example.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl">
<button class="btn" ng-click="open()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
modal.html:
<div class="modal-header">
<h3>I'm a modal!</h3>
</div>
<textarea ng-model="text"></textarea>
<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>
Run Code Online (Sandbox Code Playgroud)
为什么我不能得到ModalInstanceCtrl中定义的$ scope.text,即使我可以使用$ scope.ok和$ scope.cancel?
Alw*_*ner 72
看起来像是一个范围问题.我让它像这样工作:
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.input = {};
$scope.ok = function () {
alert($scope.input.abc);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
Run Code Online (Sandbox Code Playgroud)
HTML:
<textarea ng-model="input.abc"></textarea>
Run Code Online (Sandbox Code Playgroud)
ger*_*tas 16
2014年11月更新:使用angular-ui-bootstrap 0.12.0解决了该问题- 转换范围与控制器的范围合并.没有必要做任何事情.请留下:
<textarea ng-model="text"></textarea>
Run Code Online (Sandbox Code Playgroud)
在0.12.0之前:
Angular-UI模式使用transclusion来附加模态内容,这意味着在modal中创建的任何新作用域条目都是在子作用域中创建的.
您应该使用继承并text在父级中初始化空条目,$scope
或者您可以将输入显式附加到父作用域:
<textarea ng-model="$parent.text"></textarea>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51127 次 |
| 最近记录: |