我在 Angular 方面遇到了一些问题。现在我有以下代码:
<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>
我想知道为什么我不能在 Controller 中console.log abc 值?我的英语不好,请帮助我。提前致谢
最佳答案
尝试改变
$scope.test = function() {
console.log($scope.abc);
}
至
$scope.test = function() {
console.log(this.abc);
}
“this”将解析为当前作用域对象,并且您应该能够在不断更改文本时打印该值。
关于angularjs - 在 ng-repeat AngularJS 中使用 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549456/