$("#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> ');
$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++;
});
});
}
};
});
在controller里面可以监听回车事件,为什么要用Jq去做,这样肯定不能改变count的值啊