Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Select2“更改”处理程序中访问角度范围?_Javascript_Angularjs_Angularjs Scope_Jquery Select2_Angularjs Select2 - Fatal编程技术网

Javascript 如何在Select2“更改”处理程序中访问角度范围?

Javascript 如何在Select2“更改”处理程序中访问角度范围?,javascript,angularjs,angularjs-scope,jquery-select2,angularjs-select2,Javascript,Angularjs,Angularjs Scope,Jquery Select2,Angularjs Select2,我很想知道如何为正确的对象设置正确的属性 目前在变更事件处理程序中,我不知道我所处的范围。实际上,我不知道应该更新哪个位置 普朗克: 代码亮点 app.controller('MyCtrl', ['$scope', function ($scope) { $scope.locations = ['Paris', 'London']; }]); app.directive('timezone', function(timezones) { var _updateSetting =

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

目前在变更事件处理程序中,我不知道我所处的范围。实际上,我不知道应该更新哪个位置

普朗克:

代码亮点

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; }
    }
});
注意:我不想使用 我想更接近我的代码,掌控一切:

注意:Select2构造了一些屏幕外div,我可能会查找$e.target.parent.text,但我觉得它看起来很难看


如果您有任何建议如何正确地以角度进行此操作,请在回答/评论中告诉我:

我不确定这是否是我最喜欢的方法,但它应该适用于您感兴趣的内容:

app.directive('timezone', function(timezones) {
    var _updateSetting = function(locations) {
        console.log(locations);

    };

    return {
        restrict: 'E',

        link: function(scope, el, attrs) {
            el.select2( { data: timezones.get() } ).on("change", function() {
              _updateSetting(scope.locations);
            });
        },

        template : "<input type='hidden' class='timezoneSelector' style='width: 100%;'>"
    }
});

我不确定这是否是我最喜欢的方式,但它应该适合您感兴趣的内容:

app.directive('timezone', function(timezones) {
    var _updateSetting = function(locations) {
        console.log(locations);

    };

    return {
        restrict: 'E',

        link: function(scope, el, attrs) {
            el.select2( { data: timezones.get() } ).on("change", function() {
              _updateSetting(scope.locations);
            });
        },

        template : "<input type='hidden' class='timezoneSelector' style='width: 100%;'>"
    }
});

您可以通过在@urban_racoons的回答中将其作为参数传递来访问范围。然而,当您使用Angular之外的事件时,您必须让它知道您已经使用$apply更新了范围


您可以通过在@urban_racoons的回答中将其作为参数传递来访问范围。然而,当您使用Angular之外的事件时,您必须让它知道您已经使用$apply更新了范围


注意作用域。$applyfn--$apply不仅运行代码,而且在try/catch中运行代码,因此始终捕获错误,$digest调用位于finally子句中,这意味着无论抛出什么错误,它都将运行。这很好。是的,确保有作用域。$apply是一个很好的捕获方法,如果你要更新作用域请注意作用域。$applyfn---$apply不仅运行你的代码,而且在try/catch中运行它,所以你的错误总是被捕获的,$digest调用在finally子句中,这意味着它将在抛出错误时运行。这很好。是的,确保有范围。当然,如果你要更新范围,$apply是一个很好的捕获。我可以相当容易地访问范围,这只是我在上面定义了事件处理程序的事实。当然。我可以相当容易地访问范围,这只是我在上面定义的事件处理程序的事实。