angular.js - Why can't angular update the variables in $scope?
大家讲道理
大家讲道理 2017-05-15 17:08:10
0
1
510
$("#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('');
                    }
                });
                
                
                
                

What I want is to press Enter in the id=label input box above and update the value of $scope.count, but I don’t know why it cannot be updated. .

    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++;
                        });
                    });
                }
                };
        });
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
阿神

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template