简体   繁体   English

手动更新后单向绑定不起作用

[英]One-way binding doesn't work after manual update

Using Angular 1.5.8, I would like to have an HTML text field that always displays the value of some model value, but I need to use one-way update only (model-to-view update, but not view-to-model). 使用Angular 1.5.8,我希望有一个HTML文本字段,该文本字段始终显示某些模型值的值,但是我只需要使用单向更新(模型到视图的更新,而不是视图到模型的更新) )。 It works but if this text field is manually modified (user types something, which is allowed), the text field doesn't get updated anymore, even after a few model updates. 它可以工作,但是如果手动修改了此文本字段(用户输入了允许的值),则即使对模型进行了一些更新,文本字段也不会再更新。 Using Chrome's Developer tools, I can clearly see the value attribute of my <INPUT> being updated, even once the model is updated after a manual update, but the HTML view doesn't reflect this. 使用Chrome的开发人员工具,即使在手动更新后更新了模型,也可以清楚地看到我的<INPUT>value属性正在更新,但是HTML视图无法反映这一点。 Any idea why? 知道为什么吗?

This text field is used to display the current page number: the user can freely edit its content without any page change, but when [s]he hits the ENTER key, the entered page number is sent to the model/controller. 该文本字段用于显示当前页码:用户可以自由编辑其内容,而无需进行任何页面更改,但是当按下ENTER键时,输入的页码将发送到模型/控制器。

HTML: HTML:

<div id="pageCtrlPanel" ng-controller="PageController as pageCtrl">
    Page <input id="pageNumTxt" type="text" value="{{pageData.currPage}}">
</div>

Javascript: Javascript:

app.controller("PageController", function($scope) {
    $scope.pageData = .....;
});

app.directive('keyListener', function(dialogService) {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function(event) {
            var pageController = angular.element( $('#pageCtrlPanel') ).scope().pageCtrl;
            var target = $( event.target );
            scope.$apply(function() {
                // .......
                if ( target.is( 'input' ) ) {
                    if ( target.is( '#pageNumTxt' ) && event.which == 13 ) {
                        // ENTER key pressed on page selection text field
                        event.preventDefault();
                        var newPage = parseInt( $('#pageNumTxt').val() , 10);
                        if (!pageController.goToPage(newPage)) {
                            alert('Invalid page number (should be in [1 ~ ' + pageController.getLastPage() + ']).');
                        }
                    }
                }
            });
            scope.$digest();
        });
    };
});

As per OP's requirement (after discussing about the requirements in the comments), the model update should happen only when the Enter key is pressed. 根据OP的要求(在讨论了注释中的要求之后),仅当按下Enter键时才应该进行模型更新。

For achieving this, set ng-model and ng-model-options="{updateOn : 'change blur'}" so that the model update happens only when you tab out or similar way. 为实现此目的,请设置ng-modelng-model-options="{updateOn : 'change blur'}"以便仅当您退出或采用类似方式时才进行模型更新。 For updating the model on submit (ie when the Enter key is pressed), use ng-model-options="{updateOn : 'submit'}" 要在提交时(即,按Enter键时)更新模型,请使用ng-model-options="{updateOn : 'submit'}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM