Angular.js: ng-model은 μž…λ ₯의 초기 κ°’κ³Ό μœ„μΉ˜λ₯Ό μ„€μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ [type = range]

에 λ§Œλ“  2014λ…„ 03μ›” 18일  Β·  35μ½”λ©˜νŠΈ  Β·  좜처: angular/angular.js

기본적으둜 λ²”μœ„ μž…λ ₯은 ng-model을 μ‚¬μš©ν•˜μ—¬ 초기 κ°’μœΌλ‘œ μ„€μ •λ©λ‹ˆλ‹€. HTML μš”μ†Œμ˜ μ‹€μ œ κ°’κ³Ό λ²”μœ„ μŠ¬λΌμ΄λ”μ˜ μœ„μΉ˜κ°€ μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μŠ¬λΌμ΄λ” μœ„μΉ˜μ™€ html μž…λ ₯ 값이 λͺ¨λΈ λ³€μˆ˜μ™€ 동기화 될 κ²ƒμœΌλ‘œ μ˜ˆμƒν•©λ‹ˆλ‹€.

μ—¬κΈ° μž¬ν˜„μ΄ μžˆμŠ΅λ‹ˆλ‹€ : http://jsfiddle.net/fschwiet/HVp2J/

Windows 8μ—μ„œ μ‹€ν–‰λ˜λŠ” Chrome 및 Firefoxμ—μ„œ ν™•μΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

input [type = range]에 λŒ€ν•΄ μ—΄λ €μžˆλŠ” λ‹€λ₯Έ λ¬Έμ œκ°€ μžˆμ§€λ§Œ 관련이 λͺ…ν™•ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

forms moderate duplicate broken expected use bug

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

GitHub의 μƒˆλ‘œμš΄ λ°˜μ‘ κΈ°λŠ₯을 μ‚¬μš©ν•˜λ©΄ 더 이상 "+1"을 주석 ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€. λŒ€μ‹ μ΄ 호의 첫 번째 λŒ“κΈ€ 였λ₯Έμͺ½ μƒλ‹¨μ—μžˆλŠ” "+ : smile :"λ₯Ό 클릭 ν•œ λ‹€μŒ : +1 : ( "+1")을 μ„ νƒν•˜μ—¬ 관심을 ν‘œμ‹œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œν•˜λ©΄μ΄ λ¬Έμ œμ— κ°€μž… ν•œ λͺ¨λ“  μ‚¬λžŒμ—κ²Œ μ“Έλͺ¨μ—†λŠ” μ•Œλ¦Όμ„ λ³΄λ‚΄λŠ” 것을 방지 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“  35 λŒ“κΈ€

이것은 Angular 1.2.14의 μ—¬μ „νžˆ λ¬Έμ œμž…λ‹ˆλ‹€ : http://jsfiddle.net/HVp2J/7/

ν•΄κ²° λ°©λ²•μœΌλ‘œ $ timeout ν•Έλ“€λŸ¬ λ‚΄μ—μ„œ λͺ¨λΈ 값을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

$ timeout은 λ„μ›€μ΄λ˜μ§€ μ•Šμ§€λ§Œ λ¬Έμ œλŠ” 남아 μžˆμŠ΅λ‹ˆλ‹€.

@fschwiet : http://jsfiddle.net/HVp2J/9/

@ Rajat-Chowdhary μ£„μ†‘ν•©λ‹ˆλ‹€. $ timeout이 ν•΄κ²° λ°©λ²•μœΌλ‘œ μž‘λ™ν•˜λŠ” 방법을 λ³΄μ—¬μ£ΌλŠ” μ½”λ“œ μƒ˜ν”Œμ„ ν¬ν•¨ν•΄μ•Όν•©λ‹ˆλ‹€. http://jsfiddle.net/fschwiet/YC4av/

ν•΄κ²° 방법이 μž‘λ™ν•˜λ €λ©΄ $ timeout μ½œλ°±μ΄μžˆμ„ λ•ŒκΉŒμ§€ λͺ¨λΈ 값을 μ΄ˆκΈ°ν™” ν•  수 μ—†κΈ° λ•Œλ¬Έμ— ν•΄κ²° 방법이 어색해 λ³΄μž…λ‹ˆλ‹€.

문제λ₯Ό 찾은 것 κ°™μŠ΅λ‹ˆλ‹€.
κ°λ„λŠ” μ΅œμ†Œ, μ΅œλŒ€, 단계λ₯Ό μ„€μ •ν•˜κΈ° 전에 값을 μ„€μ •ν•˜λ―€λ‘œ λ²”μœ„κ°€ 잘λͺ»λœ μœ„μΉ˜μ—μžˆλŠ” κ²ƒμ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€. 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄μ΄ μ§€μ‹œλ¬Έμ„ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

angular.module('angular-range', []).directive('range', function() {
    return {
        replace: true,
        restrict: 'E',
        scope: {
            value: '=ngModel',
            min: '=rangeMin',
            max: '=rangeMax',
            step: '=rangeStep',
        },
        template: '<input type="range"/>',
        link: function(scope, iElement, iAttrs) {
            scope.$watch('min', function() { setValue(); });
            scope.$watch('max', function() { setValue(); });
            scope.$watch('step', function() { setValue(); });
            scope.$watch('value', function() { setValue(); });

            function setValue() {
                if (
                    angular.isDefined(scope.min) &&
                    angular.isDefined(scope.max) &&
                    angular.isDefined(scope.step) &&
                    angular.isDefined(scope.value)
                ) {
                    iElement.attr("min", scope.min);
                    iElement.attr("max", scope.max);
                    iElement.attr("step", scope.step);
                    iElement.val(scope.value); 
                }
            }
            function read() {
                scope.value = iElement.val();
            }

            iElement.on('change', function() {
                scope.$apply(read);
            });
        }
    };
});

λ‹€μŒμ€ private λ²”μœ„κ°€μ—†λŠ” μƒˆ λ²„μ „μ˜ ngRangeμž…λ‹ˆλ‹€.

.module('ngRange', [])
.directive('ngRange', function() {
    return {
        replace: true,
        restrict: 'E',
        require: 'ngModel',
        template: '<input type="range"></input>',
        link: function(scope, element, attrs, ngModel) {
            var ngRangeMin;
            var ngRangeMax;
            var ngRangeStep;
            var value;

            if (!angular.isDefined(attrs.ngRangeMin)) {
                ngRangeMin = 0;
            } else {
                scope.$watch(attrs.ngRangeMin, function(newValue, oldValue, scope) {
                    if (angular.isDefined(newValue)) {
                        ngRangeMin = newValue;
                        setValue();
                    }
                });
            }
            if (!angular.isDefined(attrs.ngRangeMax)) {
                ngRangeMax = 100;
            } else {
                scope.$watch(attrs.ngRangeMax, function(newValue, oldValue, scope) {
                    if (angular.isDefined(newValue)) {
                        ngRangeMax = newValue;
                        setValue();
                    }
                });
            }
            if (!angular.isDefined(attrs.ngRangeStep)) {
                ngRangeStep = 1;
            } else {
                scope.$watch(attrs.ngRangeStep, function(newValue, oldValue, scope) {
                    if (angular.isDefined(newValue)) {
                        ngRangeStep = newValue;
                        setValue();
                    }
                });
            }
            if (!angular.isDefined(ngModel)) {
                value = 50;
            } else {
                scope.$watch(
                    function () {
                        return ngModel.$modelValue;
                    }, 
                    function(newValue, oldValue, scope) {
                        if (angular.isDefined(newValue)) {
                            value = newValue;
                            setValue();
                        }
                    }
                );
            }

            function setValue() {
                if (
                    angular.isDefined(ngRangeMin) &&
                    angular.isDefined(ngRangeMax) &&
                    angular.isDefined(ngRangeStep) &&
                    angular.isDefined(value)
                ) {
                    element.attr("min", ngRangeMin);
                    element.attr("max", ngRangeMax);
                    element.attr("step", ngRangeStep);
                    element.val(value); 
                }
            }

            function read() {
                if (angular.isDefined(ngModel)) {
                    ngModel.$setViewValue(value);
                }
            }

            element.on('change', function() {
                if (angular.isDefined(value) && (value != element.val())) {
                    value = element.val();
                    scope.$apply(read);
                }
            });
        }
    };
});

이것은 μ—¬μ „νžˆ β€‹β€‹λ¬Έμ œμž…λ‹ˆλ‹€, κ·Έλ ‡μ£ ?

http://plnkr.co/edit/MAT5KPkXyWblAJ71L8nK?p=preview

λ„€ +1

예, μ—¬μ „νžˆ λ¬Έμ œμž…λ‹ˆλ‹€.

+1

+1, μž…λ ₯ λ²”μœ„λ₯Ό μž…λ ₯ λ²ˆν˜Έμ™€ κ²°ν•©ν•˜λ©΄ μ΅œμ•…μ΄λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

이 λ¬Έμ œμ— λŒ€ν•œ +1

같은 λ¬Έμ œμ— 직면 : +1

응-같은 문제 +1

이것은 κΈ°λ³Έ html5μž…λ‹ˆλ‹€-이것에 λŒ€ν•œ μƒνƒœλŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

이것을 κ³ λ €ν•˜μ‹­μ‹œμ˜€ : https://github.com/danielcrisp/angular-rangeslider/issues/51
였λ₯˜ : ngModel : numfmt λ²”μœ„ μŠ¬λΌμ΄λ”κ°€ throwλ©λ‹ˆλ‹€.
@ ardf69 μ†”λ£¨μ…˜μ„ λ‹€μŒκ³Ό 같이 μ‘°μ •ν–ˆμŠ΅λ‹ˆλ‹€.

angular.module('common').directive('rangeSlider', [function () {
    return {
        replace: true,
        restrict: 'E',
        require: 'ngModel',
        template: '<input type="range"/>',
        link: function (scope, element, attrs, ngModel) {
            var ngRangeMin;
            var ngRangeMax;
            var ngRangeStep;
            var value;

            function init() {
                if (!angular.isDefined(attrs.ngRangeMin)) {
                    ngRangeMin = 0;
                } else {
                    scope.$watch(attrs.ngRangeMin, function (newValue, oldValue, scope) {
                        if (angular.isDefined(newValue)) {
                            ngRangeMin = newValue;
                            setValue();
                        }
                    });
                }
                if (!angular.isDefined(attrs.ngRangeMax)) {
                    ngRangeMax = 100;
                } else {
                    scope.$watch(attrs.ngRangeMax, function (newValue, oldValue, scope) {
                        if (angular.isDefined(newValue)) {
                            ngRangeMax = newValue;
                            setValue();
                        }
                    });
                }
                if (!angular.isDefined(attrs.ngRangeStep)) {
                    ngRangeStep = 1;
                } else {
                    scope.$watch(attrs.ngRangeStep, function (newValue, oldValue, scope) {
                        if (angular.isDefined(newValue)) {
                            ngRangeStep = newValue;
                            setValue();
                        }
                    });
                }
                if (!angular.isDefined(ngModel)) {
                    value = 50;
                } else {
                    scope.$watch(
                        function () {
                            return ngModel.$modelValue;
                        },
                        function (newValue, oldValue, scope) {
                            if (angular.isDefined(newValue)) {
                                value = newValue;
                                setValue();
                            }
                        }
                    );
                }

                if (!ngModel) {
                    return;
                }
                ngModel.$parsers.push(function (value) {
                    var val = Number(value);
                    if (val !== val) {
                        val = undefined;
                    }
                    return val;
                });
            }

            function setValue() {
                if (
                    angular.isDefined(ngRangeMin) &&
                    angular.isDefined(ngRangeMax) &&
                    angular.isDefined(ngRangeStep) &&
                    angular.isDefined(value)
                    ) {
                    element.attr("min", ngRangeMin);
                    element.attr("max", ngRangeMax);
                    element.attr("step", ngRangeStep);
                    element.val(value);
                }
            }

            function read() {
                if (angular.isDefined(ngModel)) {
                    ngModel.$setViewValue(value);
                }
            }

            element.on('change', function () {
                if (angular.isDefined(value) && (value != element.val())) {
                    value = element.val();
                    scope.$apply(read);
                }
            });

            init();
        }
    };
}
]);

+1, ng-if둜 [μž…λ ₯ μœ ν˜• = λ²”μœ„]λ₯Ό μ „ν™˜ ν•  λ•Œλ„ 같은 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” λ˜ν•œμ΄ 문제둜 달리고, 그것에 λŒ€ν•΄ μŠ€νƒ μ˜€λ²„ν”Œλ‘œ μ§ˆλ¬Έμ„ 썼닀, "κ°λ„μ—μ„œ λ°”μΈλ“œ ngModel _before_ λ²”μœ„ λ³€μˆ˜μ˜ 속성은 μž…λ ₯의 κ²°ν•© value " . μ§ˆλ¬Έμ—λŠ”μ΄ 문제λ₯Ό μž¬ν˜„ν•˜λŠ” μ‹€ν–‰ κ°€λŠ₯ν•œ μ½”λ“œ 쑰각이 ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

일뢀 ν•΄κ²° 방법은 Stack Overflow에 λŒ€ν•œ λ‹΅λ³€μœΌλ‘œ κ²Œμ‹œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‚΄ κ²½μš°μ— 적용 ν•  μˆ˜μžˆλŠ” 것은 $inputElement.prop() 와 ν•¨κ»˜ DOM μ‘°μž‘μ„ μ‚¬μš©ν•˜μ—¬ min , max 및 step 값을 μ„€μ •ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€ (ν•„μš”ν•œ 속성). λ‚΄ μ‚¬μš©μž 지정 μ§€μ‹œλ¬Έμ˜ κΈ°λ³Έ <input> 의 value κ°€ 처음 μ„€μ •λ˜κΈ° 전에이 DOM μ‘°μž‘μ„ μˆ˜ν–‰ν•˜λ©΄ 문제λ₯Ό ν”Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이 버그λ₯Ό ν•΄κ²°ν•˜λŠ” 게으λ₯Έ 방법은 μ΅œμ†Œ / μ΅œλŒ€ 및 단계 값을 100으둜 λ‚˜λˆ„λŠ” κ²ƒμž…λ‹ˆλ‹€. λ”°λΌμ„œ 300은 3.0이되고 값은 100 μ•„λž˜λ‘œ λ–¨μ–΄μ§‘λ‹ˆλ‹€. 그런 λ‹€μŒ ν•„μš”μ— 따라 λ‹€μ‹œ κ³±ν•©λ‹ˆλ‹€. λˆ„κ΅°κ°€μ—κ²Œ 유용 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

+1 이것은 맀우 μ„±κ°€μ‹  μΌμž…λ‹ˆλ‹€.

+1

+1

+1

또 λ‹€λ₯Έ 게으λ₯Έ ν•΄κ²° 방법 ν•΄ν‚Ή :

      $timeout(function () {
        angular.element('.slider-input').val(amount);
      });

+1

GitHub의 μƒˆλ‘œμš΄ λ°˜μ‘ κΈ°λŠ₯을 μ‚¬μš©ν•˜λ©΄ 더 이상 "+1"을 주석 ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€. λŒ€μ‹ μ΄ 호의 첫 번째 λŒ“κΈ€ 였λ₯Έμͺ½ μƒλ‹¨μ—μžˆλŠ” "+ : smile :"λ₯Ό 클릭 ν•œ λ‹€μŒ : +1 : ( "+1")을 μ„ νƒν•˜μ—¬ 관심을 ν‘œμ‹œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œν•˜λ©΄μ΄ λ¬Έμ œμ— κ°€μž… ν•œ λͺ¨λ“  μ‚¬λžŒμ—κ²Œ μ“Έλͺ¨μ—†λŠ” μ•Œλ¦Όμ„ λ³΄λ‚΄λŠ” 것을 방지 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ng-model을 μ œκ±°ν•˜μ—¬ ondrag 방법을 μ‚¬μš©ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.
이 http://codepen.io/nagarajumusini/pen/BKpGJz 확인

+1

+1

μž…λ ₯에 ng-initλ₯Ό μΆ”κ°€ν•˜λ©΄ 초기 값을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

<input type="range" class="slider-years" min="0" max="30" step="1" value="0" data-ng-model="metier.anneeExperience" data-ng-init="metier.anneeExperience ? metier.anneeExperience = metier.anneeExperience : metier.anneeExperience = 0" />

λ‹€μŒμ—μ„œ μ†”λ£¨μ…˜μ„ μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.

http://jsfiddle.net/jestho/8dymV/7/

μš°λ¦¬λŠ” 이것을 (λ§ˆμ§€λ§‰μœΌλ‘œ) λ…Έλ ₯ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. https://github.com/angular/angular.js/issues/5892 의 볡제본으둜 λ‹«κΈ°

$ (λ¬Έμ„œ) .ready (function () {
angular.element ( '. yourclasshere'). val (valueoftheslideronload);
});

이 jQuery ν•΄κ²° 방법은 νŽ˜μ΄μ§€κ°€λ‘œλ“œ 될 λ•Œ 값을 μ„€μ •ν•  수 μžˆλ„λ‘ μž‘λ™ν•˜μ§€λ§Œ μŠ¬λΌμ΄λ” 컨트둀의 값은 ν„°μΉ˜ ν•  λ•ŒκΉŒμ§€ λ™κΈ°ν™”λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@davedx μ•„λ§ˆλ„ 이것이 λ¬Έμ œκ°€ ν•΄κ²°λ˜κΈ° 전에 κ°€μž₯ 쒋은 λ°©λ²•μž…λ‹ˆλ‹€.

<input type="range" min="15" data-ng-init="signup.age=30" value="30" max="85" step="1" name="ageInput" class="form-control input-lg" ng-model="signup.age" oninput="ageOutputId.value = ageInput.value" /> <output name="ageOutputName" id="ageOutputId">30</output>

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰