我真的很想知道如何为正确的对象设置正确的属性。

目前在“更改”事件处理程序中,我不知道我所处的范围。实际上我不知道应该更新哪个位置。
Plunker:http ://plnkr.co/edit/T4aBzND7VV2gCkui0I7P?p=preview
代码亮点
app.controller('MyCtrl', ['$scope', function ($scope) {
$scope.locations = ['Paris', 'London'];
}]);
app.directive('timezone', function(timezones) {
var _updateSetting = function(e) {
console.log(e.target.value);
// How do I update objects in my scope?
// (cannot access here)
};
return {
restrict: 'E',
link: function(scope, el, attrs) {
el.select2( { data: timezones.get() } ).on("change", _updateSetting);
},
template : "<input type='hidden' class='timezoneSelector' style='width: 100%;'>"
}
});
app.factory('timezones', function() {
var timezones = ["Europe/London", "Europe/Paris"];
var timezonesSelect = timezones.map(function(obj) { return {id: obj, text: obj }; } );
// preparing data so it is ready to use for select2
return {
get : function() { return timezonesSelect; }
}
});
注意:我不想使用https://github.com/angular-ui/ui-select2 我想更接近我的代码,控制自己:)
注意: Select2 构造了一些屏幕外的 div,我可能会查找$(e.target).parent().text(),但它对我来说看起来很难看
如果您对如何以“角度方式”正确执行此操作有任何建议 - 请在答案/评论中告诉我:)