简体   繁体   English

角度$ scope无法正常工作。 试图在控制器$ scope和$ scope.function范围内分配对象值

[英]Angular $scope not working as expected. Trying to assign object values in a controller $scope and a $scope.function scope

For context I'm trying to write some custom form validation in angularjs. 对于上下文,我试图在angularjs中编写一些自定义表单验证。 I start with some default messages that accompany the fields. 我从字段附带的一些默认消息开始。 If error is false the message appears grey (normal informative), if error is true (the message appears red (warning). The validation checks that I've omitted will change the message strings and set error to true (if the checks fail). So every time a user runs the validate function I need it to first reset $scope.registerMessage to the default form messages and then run the checks again so any fixed errors don't persist error messages. The code currently works but I've been forced to define the default message object twice and I'm sure this is unnecessary but try as I might, I can't get it to work any other way. 如果错误为假,则消息显示为灰色(正常情况),如果错误为真(消息显示为红色(警告)。我省略的验证检查将更改消息字符串并将错误设置为true(如果检查失败)。因此,每次用户运行validate函数时,我都需要先将$ scope.registerMessage重置为默认的表单消息,然后再次运行检查,以便任何固定的错误都不会保留错误消息。被迫两次定义默认消息对象,我确定这是没有必要的,但是请尝试一下,我无法使其以任何其他方式工作。

    app.controller('loginCtrl', function ($scope) {

        // default form messages
        $scope.registerMessage = {
            username : {
                message : '',
                error   : false
            },
            firstName : {
                message : '',
                error   : false
            },
            lastName : {
                message : '',
                error   : false
            },
            email : {
                message : 'You must use a valid email to register.',
                error   : false
            },
            password : {
                message : 'Your password must be atleast six characters long and contain a number or special character.',
                error   : false
            },
            reEnterPassword : {
                message : '',
                error   : false
            }
        };

        $scope.validate = function (callback) {

            // reset to default messages before validation
            $scope.registerMessage = {
                username : {
                    message : '',
                    error   : false
                },
                firstName : {
                    message : '',
                    error   : false
                },
                lastName : {
                    message : '',
                    error   : false
                },
                email : {
                    message : 'You must use a valid email to register.',
                    error   : false
                },
                password : {
                    message : 'Your password must be atleast six characters long and contain a number or special character.',
                    error   : false
                },
                reEnterPassword : {
                    message : '',
                    error   : false
                }
            };

            // Form validation Regex checks happen here
        };

    });

I've tried assigning the default values to a non $scope object variable and then setting $scope.registerMessage equal to that variable both in and outside the validate function but that variable somehow gets assigned new values. 我尝试将默认值分配给非$ scope对象变量,然后在validate函数内外将$ scope.registerMessage设置为等于该变量,但是以某种方式为该变量分配了新值。 Any ideas how this could be written without the duplication? 有没有想法如何在没有重复的情况下编写?

Try this 尝试这个

 app.controller('loginCtrl', function ($scope) {

    function defaultMessage() {
        // default form messages
         $scope.registerMessage = {
             username : {
                message : '',
                error   : false
              },
              firstName : {
                 message : '',
                 error   : false
              },
              lastName : {
                  message : '',
                  error   : false
               },
               email : {
                   message : 'You must use a valid email to register.',
                   error   : false
               },
               password : {
                    message : 'Your password must be atleast six characters long and contain a number or special character.',
                     error   : false
                },
                reEnterPassword : {
                      message : '',
                      error   : false
                 }
           };

      }


    $scope.validate = function (callback) {
       defaultMessage();
    };

   //Init
   defaultMessage();
});

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

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