search

Home  >  Q&A  >  body text

angular.js - angular 为什么更新不了$scope里面的变量

$("#label").on(
                "keydown",
                function(e) {
                    var $this = $(this)
                    if (e.keyCode == 13) {
                        var count = 1;
                        $("#labelShow span").each(function() {
                            ++count;
                        });
                        $(".count").val(count);
                        if (count > 5) {
                            return false;
                        }
                        $("#labelShow").append(
                                '<span class="label label-info">' + $this.val()
                                        + '</span>&nbsp;');
                        
                        $this.val('');
                    }
                });
                
                
                
                

想的就是上面的id=label的输入框上回车了,就更新$scope.count的值,但是不知道为啥更新不了。。

    var app = angular.module('myApp', [ 'ngAnimate' ]);
        app.controller('myCtrl', function($scope) {
            $scope.showPopupMsg = false;
            $scope.count = 0;
            $scope.$watch('count', function(newValue, oldValue){
                if($scope.count > 5) {
                    $scope.showPopupMsg = true;
                }else {
                    $scope.showPopupMsg = false;
                }
            },true);
        });
        
        app.directive('showDirective', function(){
            return {
                restrict: "E",
                scope:{
                    count:"="
                },
                template:"<input type=\"hidden\" class=\"count\" ng-model=\"count\">",
                link:function(scope, element, attrs){
                    element.bind('change', function(){
                        scope.$apply(function(){
                            scope.count++;
                        });
                    });
                }
                };
        });
大家讲道理大家讲道理2744 days ago485

reply all(1)I'll reply

  • 阿神

    阿神2017-05-15 17:10:10

    You can monitor the carriage return event in the controller. Why do you need to use Jq to do it? This will definitely not change the value of count

    reply
    0
  • Cancelreply