Javascript AngularJS+;ChosenJS更新模型
基于本教程,我正在为ChosenJS插件创建一个角度指令: 我想做的是在选择值时更新模型Javascript AngularJS+;ChosenJS更新模型,javascript,angularjs,Javascript,Angularjs,基于本教程,我正在为ChosenJS插件创建一个角度指令: 我想做的是在选择值时更新模型 function Foo($scope) { $scope.legalEntitiesList = [ { name: 'Foo' }, { name: 'Bar' } ]; $scope.legalEntity = { name: 'Foo' }; } myApp.directive('chosen', ['$timeout', function($ti
function Foo($scope) {
$scope.legalEntitiesList = [
{ name: 'Foo' },
{ name: 'Bar' }
];
$scope.legalEntity = { name: 'Foo' };
}
myApp.directive('chosen', ['$timeout', function($timeout) {
var linker = function(scope, element, attrs, ngModel) {
if (!ngModel) return;
element.addClass('chzn-select');
$(element).chosen()
.change(function(e) {
console.log(ngModel.$viewValue);
});
scope.$watch(attrs.chosen, function() {
$(element).trigger('liszt:updated');
});
}
return {
restrict: 'A',
scope: true,
require: '?ngModel',
link: linker
}
}]);
这是一把小提琴。如果选择其他选项,则不会更新模型值。如果修改选择以绑定到
legalEntity.name
,而不仅仅是legalEntity
,则小提琴可以工作
<select id="legalEntityInput" chosen="legalEntitiesList" ng-model="legalEntity.name" ng-options="legalEntity.name for legalEntity in legalEntitiesList" data-placeholder="Select..."><option></option></select>
查看此示例。如果您修改选择以绑定到
legalEntity.name
,而不仅仅是legalEntity
,您的小提琴可以工作
<select id="legalEntityInput" chosen="legalEntitiesList" ng-model="legalEntity.name" ng-options="legalEntity.name for legalEntity in legalEntitiesList" data-placeholder="Select..."><option></option></select>
请参见此示例。我想将此作为评论添加,但我缺乏信誉点。但是,请注意,新版本的Selected使用事件
Selected:updated
,而不是liszt:updated
——感谢您的视频,Dustin 我想加上这句话作为评论,但我缺乏声望点。但是,请注意,新版本的Selected使用事件Selected:updated
,而不是liszt:updated
——感谢您的视频,Dustin