如何在jQuery回调中设置ng-model

时间:2015-02-02 11:21:02

标签: javascript jquery angularjs

<script>
    $("#point-range").ionRangeSlider({
        min: 0,
        max: 10,
        from: 1,
        from_min: 1,
        from_max: 10,
        step: 1,
        grid: true,
        hide_min_max: true,
        onFinish: function (data) {
            $('#point-value').val(data.from);
        }
    });
</script>

如何在调用ng-model函数时为onFinish设置值。我尝试将值设置为隐藏输入:

onFinish: function (data) {
   $('#point-value').val(data.from);
}

{{ Form::hidden('point', null, ['ng-model' => 'evaluationData.point', 'id' => 'point-value']) }}

但是当我提交表单时,我不会evaluationData.point

1 个答案:

答案 0 :(得分:2)

尝试将该脚本包装在指令中。

angular.module('myApp').directive('myDirective', function() {
    return {
        scope: 'ngModel',
        link: function(scope, element, attrs) {
            $("#point-range").ionRangeSlider({
                min: 0,
                max: 10,
                from: 1,
                from_min: 1,
                from_max: 10,
                step: 1,
                grid: true,
                hide_min_max: true,
                onFinish: function (data) {
                    $('#point-value').val(data.from);
                    scope.ngModel = 'Blah';
                    scope.$digest();
                }
            });
        }
    }
})