1
Я хотел бы использовать $watchCollection
, чтобы отслеживать 2 вещи в моей директиве.
это
ng-model
.это атрибут
id
.
Я знаю, как смотреть каждый отдельно.
return {
restrict: 'A',
require: 'ngModel',
scope: {
options: '=',
model: '=ngModel'
},
link: function ($scope, $el, $attrs) {
scope = $scope;
$scope.$watch(
function() {
return $el[0].id;
},
function (elementId) {
$('#' + elementId).on('switch-change', function() {
scope.model[this.id] = $(this).find('input').is(':checked');
$scope.$apply();
});
$('#' + elementId)['bootstrapSwitch']();
$('#' + elementId).bootstrapSwitch('setOnLabel', _($('#' + elementId).attr('data-on-label')));
$('#' + elementId).bootstrapSwitch('setOffLabel', _($('#' + elementId).attr('data-off-label')));
});
$scope.$watchCollection('model', function(newval){
if(typeof newval !== 'undefined' && !$.isEmptyObject(newval)){
// do some stuff here
}
});
}
}
Но я хотел бы получить все переменные в одной функции .. Спасибо