【发布时间】:2015-08-27 06:43:05
【问题描述】:
我在 Angular 中遇到绑定问题。
HTML:
<div ng-controller="myCtrl">
<li> Discount
<input ng-change="update_amount()" type="number" ng-model="discount" />
</li>
<li> Other Charges
<input ng-change="update_amount()" type="number" ng-model="others" />
</li>
</div>
JS:
ng.module('app').controller('myCtrl', [
'$scope',
function ($scope) {
$scope.removeRestaurantFromActiveCall = function () {
$scope.updateActiveCall(function (call) {
call.restaurant = null;
});
};
$scope.showModal = false ;
$scope.others = 0 ;
$scope.discount = 0 ;
$scope.update_amount = function () {
console.log('Change called') ;
console.log($scope.others) ; // Always zero
console.log($scope.discount) ; // Always zero
console.log($scope) ; // Does not have the current value for my fields
console.log(this) ; // Has current values for my field
console.log($scope === this) ; // False
console.log($scope == this) ; // False
} ; }) ;
我在this 中获得了我的字段的值,但在scope 中没有。甚至不确定this 在这里指的是什么。
我哪里出错了?
【问题讨论】:
-
你确定上面的 sn-p 工作正常吗?您的控制器声明与标记不匹配。
-
这里您的控制器名称是
myCtrl并且您正在尝试访问另一个控制器上的属性app.leftPane.restaurant我的错。控制器名称在实际代码中很好。只是想在这里缩短它。忘记在这两个地方进行更改为了简洁起见,这只是代码的一部分。实际代码没有语法错误。
标签: angularjs