简体   繁体   English

ng-show后AngularJS ng-form输入字段不显示数据

[英]AngularJS ng-form input fields not displaying data after ng-show

I'm working on a wizard style view that hides sections of a form in order to provide the form in wizard style chunks. 我正在使用一个向导样式视图,该视图可隐藏表单的各个部分,以便以向导样式块的形式提供表单。

As you progress through the wizard and change panes the model behind the form is being updated, however if you go back to a previous pane the data no longer shows up in the input fields, but the Form still shows the $modelvalue and $viewvalue still populated with the data that was entered, the input just hasn't been updated. 当您继续执行向导并更改窗格时,将更新窗体后面的模型,但是,如果返回到上一个窗格,则数据将不再显示在输入字段中,但是窗体仍显示$ modelvalue和$ viewvalue用输入的数据填充,则输入尚未更新。

I've created a plnkr that replicates the issue at http://plnkr.co/gG29JGa7o12GlBDo3sGm 我创建了一个plnkr,可在http://plnkr.co/gG29JGa7o12GlBDo3sGm复制该问题

and here is the code for the controller: 这是控制器的代码:

function wizard_controller($scope) {
    // Bindable properties and functions are placed on vm.
    $scope.title = 'franchisee_controller';
    $scope.steps = [{name: 'Chain Info', visible: true},
        {name: 'Franchise Info', visible: false},
        {name: 'Contact Info', visible: false},
        {name: 'Billing Info', visible: false},
        {name: 'Terms Info', visible: false}];
    $scope.currentStep = 0;
    $scope.franchisee = new Franchisee();
    $scope.franchisee.Abbr = 'fatcow';
    $scope.franchisee.Name = 'Fat Cow';
    $scope.franchisee.CompanyAddress1 = '600 Parker Square';

    $scope.isCurrentStep = function(index){
        if(index === $scope.currentStep){
            return 'current';
        }

        return '';
    };

    $scope.previousDisabled = function () {
        if ($scope.currentStep !== 0) {
            return false;
        }
        return true;
    };

    $scope.nextDisabled = function () {
        if ($scope.currentStep !== $scope.steps.length) {
            return false;
        }
        return true;
    };

    $scope.previous = function () {
        $scope.steps[$scope.currentStep].visible = false;
        $scope.currentStep -= 1;
        if ($scope.currentStep < 0) {
            $scope.currentStep = 0;
        }
        $scope.steps[$scope.currentStep].visible = true;
    };

    $scope.next = function(){
        $scope.steps[$scope.currentStep].visible = false;
        $scope.currentStep += 1;
        if ($scope.currentStep >= $scope.steps.length) {
            $scope.currentStep = $scope.steps.length - 1;
        }
        $scope.steps[$scope.currentStep].visible = true;
    };

    $scope.displaySubmit = function(){
        if ($scope.currentStep !== $scope.steps.length - 1) {
            return false;
        }
        return true;
    };

    //#region click handlers
    $scope.submitForm = function (model) {
        if ($scope.franchiseeWizard.$valid) {
            angular.extend($scope.franchisee, model);
            console.log($scope.franchisee);
        }
    };
    //#endregion

    //#region message handlers

    //#endregion

    //#region init methods
    $scope.init = function () {
    };

    $scope.init();

    //#endregion
}

Does anyone have an idea on how I can get the input fields to retain the data so the user can go back and forth in the wizard. 是否有人对我如何获取输入字段来保留数据有想法,以便用户可以在向导中来回移动。 Using ng-switch isn't much of an option since it requires the use of $parent to bind the ng-model for each of the input fields. 使用ng-switch不是很多选择,因为它需要使用$ parent为每个输入字段绑定ng-model。

So this seems to be an issue with the way the browser form element is handling the hiding and showing of input elements. 因此,这似乎与浏览器表单元素处理输入元素的隐藏和显示的方式有关。

If you change the form tag and replace it with a div and ng-form attribute then everything works fine without an issue. 如果您更改表单标签,并用div和ng-form属性替换它,则一切正常,没有问题。 The reason I want to use forms is to take advantage of the form validation. 我要使用表单的原因是要利用表单验证。 Also, now that this works I can utilize this same scheme in a directive that removes all of the previous next handling out of the controller. 而且,既然这行得通,我可以在指令中利用相同的方案,该指令将所有先前的下一个处理从控制器中删除。

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

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