角度自定义输入数字不起作用

时间:2014-10-27 10:33:18

标签: angularjs angularjs-directive

我真的不明白这段代码中的问题是什么:

app.directive('counter', function() {
                return {
                    restrict: 'A',
                    scope:{},
                    template:'<div class="item-counter"><input type="text" data-ng-model="qnt"><span class="glyphicon glyphicon-chevron-up" data-ng-click="increment()"><span class="glyphicon glyphicon-chevron-down" data-ng-click="decrement()"></span><button type="button" class="btn btn-success">Aggiungi</button></div>',
                    controller: function($scope) {
                        $scope.qnt = 1;
                        $scope.increment = function() {
                            $scope.qnt++;
                        };
                        $scope.decrement = function() {
                            console.log($scope.qnt > 1);
                            if ($scope.qnt > 1) {
                                $scope.qnt--;
                            }
                            console.log($scope.qnt);
                        };
                    },
                    link: function(scope, element, attrs) {

                    }

                };
            });

增量起作用,减量不起作用。 怎么了?

http://plnkr.co/edit/BdFHpnrJnG4DFjTkmuZ0?p=preview

1 个答案:

答案 0 :(得分:3)

您没有任何增量范围的结束标记。因此,当执行减量代码时,增量代码也在执行,使减量无效。

template:'<div class="item-counter"><input type="text" data-ng-model="qnt"><span class="glyphicon glyphicon-chevron-up" data-ng-click="increment()"></span><span class="glyphicon glyphicon-chevron-down" data-ng-click="decrement()"></span><button type="button" class="btn btn-success">Aggiungi</button></div>',

更新了plunk here