- 首页
- 其他开发
- 使用指令集成 Angular 和 JQuery.iCheck 不起作用
使用指令集成 Angular 和 JQuery.iCheck 不起作用
[英] Integration of Angular and JQuery.iCheck by using a directive is not working
本文介绍了使用指令集成 Angular 和 JQuery.iCheck 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试集成 JQuery.iCheck(用于 checkbox&Radio 按钮
样式的插件).我在这里遵循了一些建议,说集成 jQuery 插件以与 Angular ngRepeat
完全兼容的方法是使用 directives
.
所以我实现了一个指令
:
webApp.directive('icheck', function($timeout, $parse) {返回 {链接:函数($scope,元素,$attrs){返回 $timeout(function() {var ngModelGetter,值;ngModelGetter = $parse($attrs['ngModel']);value = $parse($attrs['ngValue'])($scope);返回 $(element).iCheck({checkboxClass: 'icheckbox_minimal',radioClass: 'radio_minimal-grey',checkboxClass: 'icheckbox_minimal-grey',增加面积:'20%'}).on('ifChanged', function(event) {if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {$scope.$apply(function() {返回 ngModelGetter.assign($scope, event.target.checked);});}if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {返回 $scope.$apply(function() {返回 ngModelGetter.assign($scope, value);});}});});}};});
这个 directive
不能完全与我的 ngRepeat 元素
一起使用并且不会改变object
vote
的属性.
我的代码 - Git
我的代码:
var webApp = angular.module('webApp', []);//控制器webApp.controller('VotesCtrl', function ($scope, Votes) {$scope.votes = 票数;$scope.statuses = ["Approved","Pending","Trash","Spam"];$scope.selectedID = 1;$scope.expand = 函数(投票){控制台日志(显示");$scope.vote = 投票;$scope.selectedID = vote.id;};$scope.change = function() {for(var i = 0; i < $scope.votes.length; i++) {如果($scope.votes[i].cb){$scope.votes[i].status = $scope.votes.status;$scope.votes[i].cb = false;}$scope.show = false;}};});//服务webApp.factory('投票', [function() {//临时存储库直到与数据库集成这将被转换为restful get查询投票数 = [{编号:'1',已创建:1381583344653,更新:'222212',评级 ID: '3',率:5,ip: '198.168.0.0',状态:'待定',用户标识:'IP-地址'},{编号:'111',已创建:1381583344653,更新:'222212',评级 ID: '4',率:5,ip: '198.168.0.1',状态:'垃圾邮件',userIdentification:'FLASH-COOKIES'},{编号:'2',已创建:1382387322693,更新:'222212',评级 ID: '3',率:1,ip: '198.168.0.2',状态:已批准",userIdentification:'HTTP-COOKIES'},{编号:'4',已创建:1382387322693,更新:'222212',评级 ID: '3',率:1,ip: '198.168.0.3',状态:'垃圾邮件',用户标识:'IP-地址'}];返回选票;}]);webApp.directive('icheck', function($timeout, $parse) {返回 {链接:函数($scope,元素,$attrs){返回 $timeout(function() {var ngModelGetter,值;ngModelGetter = $parse($attrs['ngModel']);value = $parse($attrs['ngValue'])($scope);返回 $(element).iCheck({checkboxClass: 'icheckbox_minimal',radioClass: 'radio_minimal-grey',checkboxClass: 'icheckbox_minimal-grey',增加面积:'20%'}).on('ifChanged', function(event) {if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {$scope.$apply(function() {返回 ngModelGetter.assign($scope, event.target.checked);});}if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {返回 $scope.$apply(function() {返回 ngModelGetter.assign($scope, value);});}});});}};});
我的 HTML:
<div><ul><li class="check" ng-click=""><input type="checkbox" ng-model="master"/><li class="创建"><a>创建</a><b>IP地址</b><b>状态</b><ul ng-repeat="投票"><li class="check"><input type="checkbox" ng-model="vote.cb" ng-checked="master"/><li class="created" ng-class="{selected: vote.id == selectedID}"><a href="#" ng-click="expand(vote)">{{vote.created|date}}</a>{{vote.ip}}{{vote.status}}
<br/><br/><div class="details" ng-init="expand(votes[0])"><h3>详情:</h3><div>日期:{{vote.created |日期}}
<div>IP:{{vote.ip}}</div><div >饼干:<div><input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="FLASH-COOKIES"/><span>FLASH COOKIES</span>
<div><input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="HTTP-COOKIES"/><span>HTTP COOKIES</span>
<div><input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="IP-ADDRESS"/><span>IP地址</span>
解决方案
已在此链接上解决:https://github.com/fronteed/iCheck/issues/62 wajatimur>
webApp.directive('icheck', function($timeout, $parse) {返回 {要求:'ngModel',链接:函数($scope,元素,$attrs,ngModel){返回 $timeout(function() {无功值;value = $attrs['value'];$scope.$watch($attrs['ngModel'], function(newValue){$(element).iCheck('更新');})返回 $(element).iCheck({checkboxClass: 'icheckbox_flat-aero',radioClass: 'radio_flat-aero'}).on('ifChanged', function(event) {if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {$scope.$apply(function() {返回 ngModel.$setViewValue(event.target.checked);});}if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {返回 $scope.$apply(function() {返回 ngModel.$setViewValue(value);});}});});}};});
I'm trying to integrate JQuery.iCheck (plugin for checkbox&Radio buttons
styling).
I followed few suggestions here that said that the way to integrate a jQuery plugins to be fully compatible with Angular ngRepeat
is by using directives
.
So I implemented a directive
:
webApp.directive('icheck', function($timeout, $parse) {
return {
link: function($scope, element, $attrs) {
return $timeout(function() {
var ngModelGetter, value;
ngModelGetter = $parse($attrs['ngModel']);
value = $parse($attrs['ngValue'])($scope);
return $(element).iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal-grey',
checkboxClass: 'icheckbox_minimal-grey',
increaseArea: '20%'
}).on('ifChanged', function(event) {
if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
$scope.$apply(function() {
return ngModelGetter.assign($scope, event.target.checked);
});
}
if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
return $scope.$apply(function() {
return ngModelGetter.assign($scope, value);
});
}
});
});
}
};
});
This directive
does not fully work with my ngRepeat elements
and does not change
the properties of the object
vote
.
My code - Git
My code:
var webApp = angular.module('webApp', []);
//controllers
webApp.controller ('VotesCtrl', function ($scope, Votes) {
$scope.votes = Votes;
$scope.statuses = ["Approved","Pending","Trash","Spam"];
$scope.selectedID = 1;
$scope.expand = function(vote) {
console.log("show");
$scope.vote = vote;
$scope.selectedID = vote.id;
};
$scope.change = function() {
for(var i = 0; i < $scope.votes.length; i++) {
if($scope.votes[i].cb) {
$scope.votes[i].status = $scope.votes.status;
$scope.votes[i].cb = false;
}
$scope.show = false;
}
};
});
//services
webApp.factory('Votes', [function() {
//temporary repository till integration with DB this will be translated into restful get query
var votes = [
{
id: '1',
created: 1381583344653,
updated: '222212',
ratingID: '3',
rate: 5,
ip: '198.168.0.0',
status: 'Pending',
userIdentification:'IP-ADDRESS'
},
{
id: '111',
created: 1381583344653,
updated: '222212',
ratingID: '4',
rate: 5,
ip: '198.168.0.1',
status: 'Spam',
userIdentification:'FLASH-COOKIES'
},
{
id: '2',
created: 1382387322693,
updated: '222212',
ratingID: '3',
rate: 1,
ip: '198.168.0.2',
status: 'Approved',
userIdentification:'HTTP-COOKIES'
},
{
id: '4',
created: 1382387322693,
updated: '222212',
ratingID: '3',
rate: 1,
ip: '198.168.0.3',
status: 'Spam',
userIdentification:'IP-ADDRESS'
}
];
return votes;
}]);
webApp.directive('icheck', function($timeout, $parse) {
return {
link: function($scope, element, $attrs) {
return $timeout(function() {
var ngModelGetter, value;
ngModelGetter = $parse($attrs['ngModel']);
value = $parse($attrs['ngValue'])($scope);
return $(element).iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal-grey',
checkboxClass: 'icheckbox_minimal-grey',
increaseArea: '20%'
}).on('ifChanged', function(event) {
if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
$scope.$apply(function() {
return ngModelGetter.assign($scope, event.target.checked);
});
}
if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
return $scope.$apply(function() {
return ngModelGetter.assign($scope, value);
});
}
});
});
}
};
});
My HTML:
<body ng-controller="VotesCtrl">
<div>
<ul>
<li class="check" ng-click="">
<input type="checkbox" ng-model="master" />
</li>
<li class="created">
<a>CREATED</a>
</li>
<li class="ip">
<b>IP ADDRESS</b>
</li>
<li class="status">
<b>STATUS</b>
</li>
</ul>
<ul ng-repeat="vote in votes">
<li class="check">
<input type="checkbox" ng-model="vote.cb" ng-checked="master" />
</li>
<li class="created" ng-class="{selected: vote.id == selectedID}">
<a href="#" ng-click="expand(vote)">{{vote.created|date}}</a>
</li>
<li class="ip">
{{vote.ip}}
</li>
<li class="status">
{{vote.status}}
</li>
</ul>
</div>
<br />
<br />
<div class="details" ng-init="expand(votes[0])">
<h3>Details:</h3>
<div>DATE: {{vote.created | date}}</div>
<div>IP: {{vote.ip}}</div>
<div >
Cookies:
<div>
<input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="FLASH-COOKIES" />
<span>FLASH COOKIES</span>
</div>
<div>
<input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="HTTP-COOKIES" />
<span>HTTP COOKIES</span>
</div>
<div>
<input icheck type="radio" ng-model="vote.userIdentification" name="iCheck" value="IP-ADDRESS" />
<span>IP ADDRESS</span>
</div>
</div>
</div>
</body>
解决方案
Already solved on this link: https://github.com/fronteed/iCheck/issues/62 by wajatimur
webApp.directive('icheck', function($timeout, $parse) {
return {
require: 'ngModel',
link: function($scope, element, $attrs, ngModel) {
return $timeout(function() {
var value;
value = $attrs['value'];
$scope.$watch($attrs['ngModel'], function(newValue){
$(element).iCheck('update');
})
return $(element).iCheck({
checkboxClass: 'icheckbox_flat-aero',
radioClass: 'iradio_flat-aero'
}).on('ifChanged', function(event) {
if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
$scope.$apply(function() {
return ngModel.$setViewValue(event.target.checked);
});
}
if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
return $scope.$apply(function() {
return ngModel.$setViewValue(value);
});
}
});
});
}
};
});
这篇关于使用指令集成 Angular 和 JQuery.iCheck 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文