我在角度方面遇到了一些问题。现在我有以下代码:
<div ng-repeat='item in items'>
<span>{{item.title}}</span>
<input ng-change="test()" ng-model='abc'> {{abc}}
<span>{{item.price| currency}}</span>
<span>{{item.price * item.quantity| currency}}</span>
<button ng-click="remove($index)">Remove</button>
</div>
<script type="text/javascript" src="libs/angular.min.js"></script>
<script>
function CartController($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.test = function() {
console.log($scope.abc);
}
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}
</script>
我想知道为什么我无法控制器中的console.log abc值?我的英语不好,请帮助我。提前致谢
答案 0 :(得分:3)
尝试更改
$scope.test = function() {
console.log($scope.abc);
}
到
$scope.test = function() {
console.log(this.abc);
}
&#34;这&#34;将解析为当前范围对象,您应该能够在不断更改文本时打印该值。
答案 1 :(得分:0)
如果在尝试使用abc
作为变量之前会发生什么?
function CartController($scope){
var $scope.abc = 'foo';
...