Javascript 角度指令select2模糊事件未按预期工作
我有以下指示:Javascript 角度指令select2模糊事件未按预期工作,javascript,jquery,html,css,angularjs,Javascript,Jquery,Html,Css,Angularjs,我有以下指示: angular.module('click-to-edit-select2', []) .directive("clickToEditSelect2", function() { var editorTemplate = '<td id="4" class="click-to-edit-select2">' + '<div id="3" style="height:20px" ng-click="enableEditor()" ng-hide="view
angular.module('click-to-edit-select2', [])
.directive("clickToEditSelect2", function() {
var editorTemplate = '<td id="4" class="click-to-edit-select2">' +
'<div id="3" style="height:20px" ng-click="enableEditor()" ng-hide="view.editorEnabled">' +
'{{value}} ' +
'</div>' +
'<div id="2" ng-show="view.editorEnabled" style="padding:0px 10px 0px 0px";>' +
'<input id="1" type="hidden" ui-select2="select2Options" ng-model="view.editableValue" ng-change="changeText()" />' +
'</div>' +
'</td>';
return {
restrict: "A",
replace: true,
template: editorTemplate,
scope: {
value: "=clickToEditSelect2"
},
controller: function($scope, $element, $attrs) {
$scope.view = {
//editableValue: $scope.term.TermId.id,
editorEnabled: false
};
$scope.enableEditor = function() {
if ($scope.$parent.term.Status == "new") {
$scope.view.editorEnabled = true;
$scope.view.editableValue = $scope.value;
}
};
$scope.disableEditor = function() {
$scope.view.editorEnabled = false;
};
$scope.save = function() {
//alert($scope.term.TermId.id);
$scope.value = $scope.view.editableValue.id;
$scope.disableEditor();
};
$scope.$watch('view.editableValue', function(newVal, oldVal) {
if (newVal != undefined && newVal != "") {
if (oldVal == newVal) return;
$element.addClass('valueChangedTD');
var str = newVal.text;
var res = str.split(" - ");
//slice the termID
res.splice(res.length - 1, 1);
var termDescription = res.join(' - ');
}
}, true);
//select2 has its own blur event !
$element.on('select2-blur', function(event) {
$scope.save();
});
var initSelectionCb = function(item, callback) {
if (item != "") {
var id = item.val();
var data = { id: id, text: id };
callback(data);
}
};
$scope.select2Options = {
placeholder: "Select Terminal",
dropdownAutoWidth: 'true',
multiple: false,
width: "resolve",
selectOnBlur: true, //this does not seem to work
initSelection: function(item, callback) {
//selects the initial item
initSelectionCb.call(self, item, callback);
},
ajax: {
url: "/GetDataSelect2",
type: "POST",
contentType: "application/json",
data: function(term, page) {
var Json = {};
Json.term = term;
Json.page = page;
Json.limit = 10;
return Json;
},
results: function(data, page) {
return { results: data.options };
}
}
}
}
};
})
使用select2 3.4.8
引导程序2.3.1
角度1.2.19
正如您所看到的,我已经设置了onSelectBlur选项,我尝试了“true”和“true”,但它不起作用
我为select2 blur事件添加了一个事件处理程序,但它没有按预期工作
我希望单击select2 ddl,选择一个选项,然后单击tab或单击某处,并隐藏带有select2的div,这是$scope.view.editorEnabled设置为false时应该发生的情况
取而代之的是,在从ddl中选择一个项目,然后在其他地方进行制表或单击后,具有select2 ddl的div不会被隐藏。如果我第二次从select2 ddl中选择一个项目,则带有select2 ddl的div将被隐藏,并且带有所选值的div将显示
我可以看到,通过设置断点,select2 blur事件每次都会被调用,但div不会立即更新。只有当你跑两次的时候
我真的不想将select2 blur事件附加到$element(TD)上,但我不知道该怎么做。我已尝试将事件附加到我找到的其他各种元素。选择2选项、.2容器,但我甚至无法启动事件
现在已经过了几个小时了,我想我需要一双新鲜的眼睛。这可能是我没有看到的一些简单的东西
谢谢大家! 结果是一个非常简单的$scope.$apply here:
//select2 has its own blur event !
$element.on('select2-blur', function(event) {
$scope.save();
$scope.$apply();
});
答案是这样的。我想这与$scope值在事件回调中被更改有关