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

浏览:28日期:2022-12-29

问题描述

$('#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' ng-model='count'>',link:function(scope, element, attrs){ element.bind(’change’, function(){scope.$apply(function(){ scope.count++;}); });}};});

问题解答

回答1:

在controller里面可以监听回车事件,为什么要用Jq去做,这样肯定不能改变count的值啊

相关文章: