在我看来,我有一个输入,一个跨度,像这样一个按钮:
<script type="text/ng-template" id="myTemplate.html">
<input type="text" ng-model="phoneNumber">
<span>{{ phoneNumber}}</span>
<input type="button" ng-click="click()">
</script>
当在文本框中键入的内容span
更新为预期阅读。但点击该按钮时,phoneNumber
尚未更新控制器内:
app.controller('myPopopCtrl', ['$scope', '$modalInstance',
function ($scope, $modalInstance) {
$scope.phoneNumber= '';
$scope.click = function() {
alert($scope.phoneNumber); // alerts only ''
};
是有一些newbe错误,你可以在角使这使得东西不是在控制器内的$scope
更新?
我需要注意的angular-ui modal是否存在$ scope问题?
编辑:
好像phoneNumber
获取2个作用域创建。一次在蓝色箭头的范围内,其中phoneNumber: ''
和一次在子范围内的红色箭头。视图使用在儿童范围phoneNumber
和控制器使用父范围phoneNumber
...
为什么两个作用域创建?
定义的点击方法在哪里? – Chandermani
点击方法是在popupController里面定义的(我更新了这个问题来澄清) – Cotten
然后,更好的选择是传递一个对象而不是字符串,因为字符串分配会在子范围内创建一个新字符串。创建类似'$ scope.phone = {number:null}'的东西并传给它。 – Chandermani