1

I have an angular function like this:

$scope.colorValidator = function () {

    $scope.token_style = "";
    $scope.expdate_style = "";

    if (!$scope.$$childHead.billingblock.accountID.$valid) {
        $scope.token_style = {border: "1px solid #ff0000"}
    }

    if (!$scope.$$childHead.billingblock.expDate.$valid) {
        $scope.expdate_style = {border: "1px solid #ff0000"}
    }

};

How can i set the {border: "1px solid #ff0000"} in a constant variable (maybe like red), and set it in each validation ?

Edit: I'm setting ng-style (in the view) for each input.

3
  • Instead of making a constant in JavaScript, you should consider creating a CSS class .valid-field and using the JavaScript to apply that as necessary. Styles belong in stylesheets, not in code. Commented Jul 24, 2014 at 16:13
  • Oh, and how can i add it to the angular code? like: $scope.expdate_style = 'valid-field' ? Commented Jul 24, 2014 at 16:16
  • Looks like Angular has a class predefined for you: .ng-invalid. See @vittore's answer. Commented Jul 24, 2014 at 16:21

3 Answers 3

1

You do not need to do that.

Check you markup when fields are not valid, they have ng-dirty and ng-invalid classes applied

Use them to style your controls

.my-special-form .ng-invalid {
    border: 1px solid #ff0000
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now, when the page loads it shows the ng-invalid for all required fields.
1

Please see here: http://jsbin.com/hekaz/3/edit?css,output

ie:

input.ng-invalid[ng-model="username"] {
  border: 5px solid red;
}
input.ng-valid[ng-model="username"] {
  border: 5px solid green;
}

Comments

-1

I solved it setting it a variable:

var redColor = {border: "1px solid #ff0000"};

if (!$scope.$$childHead.billingblock.accountID.$valid) {
     $scope.token_style = redColor;
}

Thank you to everyone for each answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.