2014-09-23 50 views
3

當我按下按鈕檢查啓動模式時,我想在我的控制檯中打印我的文本框的值。但我的文本框返回一個未定義的。看來,所有的代碼工作都很完美。AngularJS textbox返回未定義值

這是鏈接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> 

回答

5

因爲你的模式設置有它自己的控制器,因此,其自身的範圍,

當你執行功能「檢查」它試圖提醒$範圍.new,

$ scope.new未在模式範圍上設置。

解決此問題的一種方法是將新變量傳遞給函數,並讓函數提醒傳遞的值。

這裏有一個固定的plunkr

調用時檢查(),通過在新的變量:

<button ng-click="check(new)">check</button> 

,並在控制器的變化檢查功能:

$scope.check = function(text) { 
    alert(text); 
}; 
+0

謝謝你這是工作... – 2014-09-24 06:39:28

+0

如果解決了您的問題,請接受答案。這是你如何做到這一點:http://meta.stackexchange.com/a/5235 @BharathKumar – 2014-09-24 07:20:25