请查看此fiddle here。我从最初检查过的chekbox上的角度变化事件中得到了这种奇怪的行为。我也使用jquery检查过这个。 jquery事件正常触发,而angular事件仅在最初未选中checked时触发。
这是我的完整代码btw:
link.onclick = function(){
答案 0 :(得分:1)
您可以使用纯角度执行此操作 - 不需要jQuery。
删除您的ng-change
和ng-checked
属性,然后使用ng-model
:
<input type="checkbox" ng-model="formElem.checkbox"/>
然后使用ng-show
和模型切换文字:
<p ng-show="formElem.checkbox">checked!</p>
<p ng-show="!formElem.checkbox">not checked.</p>
答案 1 :(得分:0)
删除了ng-checked
属性,更改了$scope.formElem.checkbox
的类型,更正了if ($scope.formElem.checkbox ? 'Checked' : 'Unchecked')
var app = angular.module('testing', []);
app.controller('ChgCtrl', function ($scope) {
$scope.formElem = {
checkbox: true
};
$scope.message_change = '';
$scope.toggleChange = function (data) {
$scope.message_change = ($scope.formElem.checkbox ? 'Checked' : 'Unchecked');
console.info(data === true ? 'Checked' : 'Unchecked');
};
});
$(function () {
$('input[type=checkbox]').on('change', function (e) {
$('#jq-messages').html($(this).is(':checked') ? 'Checked' : 'Unchecked');
console.log($(this).is(':checked'));
});
});
&#13;
<body ng-app="testing">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<div class="row" ng-controller="ChgCtrl">
<div class="col-md-12">
<form>
<div class="form-group">
<span class="text-info">ng-change</span>
<input type="checkbox" ng-model="formElem.checkbox" ng-change="toggleChange(formElem.checkbox)" />
</div>
</form>
</div>
<div class="col-md-12">
<div class="col-md-6">
<h3>JQuery change event:</h3>
<P id="jq-messages"></p>
</div>
<div class="col-md-6">
<h3>Angular change event:</h3>
<p>{{message_change}}</p>
</div>
<p class="text-danger">Please note the first click on checkbox.</p>
</div>
</div>
</body>
&#13;
ng-change
事件处理程序,顾名思义,将触发目标值更改,但您只是初始化值,而不是更改它们
答案 2 :(得分:0)
我的建议是不要使用Jquery并使用所有angularJS。如果您想查看更改,也可以在控制器中使用$scope.$watch
。
她是demo。