Bha*_*mar 3 javascript undefined-behavior angularjs
当我按下我的按钮检查bootstrap模式时,我想在我的控制台中打印我的文本框的值.但我的文本框返回一个未定义的.似乎所有的代码都是完美的.
这是链接Plunker
<!doctype html>
<html ng-app="app" ng-controller="ModalDemoCtrl">
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
<script src="example1.js"></script>
</head>
<body>
<div ><button class="btn btn-default" ng-click="open('lg')" >Large modal</button>
<script type="text/ng-template" id="myModalContent">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<input type="text" ng-model="new" /><button ng-click="check()" >check</button>
<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>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
由于您的模态设置为拥有自己的控制器,因此具有自己的范围,
当你执行'check'函数时,它会尝试提醒$ scope.new,
$ scope.new未在模态范围上设置.
克服这个问题的一种方法是将新变量传递给函数,并让函数警告传递的值.
这是一个固定的plunkr
调用check()时,传入新变量:
<button ng-click="check(new)">check</button>
Run Code Online (Sandbox Code Playgroud)
并在您的控制器中更改检查功能:
$scope.check = function(text) {
alert(text);
};
Run Code Online (Sandbox Code Playgroud)