0

I have an input value that I want to style, based on the value I receive back from the angular variable. So in my cshtml, I have:

 <input type="{{d.input_type}}" ng-style={{d.input_type}} == "radio" ? 'width:40px;' : 'width:100px;" @*class="form-control"*@ @*ng-model="Customer.CustStatus"*@ name="question_answer" id="question_answer" required >

Then in my angular .js file I have the following:

 $scope.Question = {
                Question1: '',
                input_type: ''
            }

            $http.get('/Home/getUserInfo')
    .then(function (response) {
        $scope.Question = response.data;
        console.log("status:" + response.status);
        console.log("data: " + $scope.Question);

    }).catch(function (response) {
        console.error('Error occurred:', response.status, response.data);
    }).finally(function () {
        console.log("Task Finished.");
    });

Based on the value I receive back (which is either "radio" or "text"), I want to have different styling. For example, I want to have a radio button height of 20px and text must be 40px. How would I go about having conditional styling on my input value, based on the text value I receive back from "input_type".

Any help would be greatly appreciated.

1 Answer 1

3

The syntax is

<input ... ng-style="{'width': d.input_type == 'radio' ? '40px' : '100px'}" ...>

So you first specify the property you're going to set and then its value based on your logic.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @NikolajDamLarsen, I copied the code from the question and didn't noticed the double brackets :)
and I wouldn't need to put d.input_type between two {}?
@user1397978 No, Angular already assumes everything passed to the ng-style directive is a javascript expression, so it doesn't need to interpolate it using {{ }}.
Nikolaj was faster with the answer but that's the logic :)
@user1397978 sure, just wrap the statement inside brackets so you're sure it gets evaluated correctly (d.input_type == 'radio' || d.input_type == 'checkbox')?...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.