user1397978
user1397978

Reputation: 265

Dynamically style based on angular value passed through

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.

Upvotes: 0

Views: 54

Answers (1)

Luca De Nardi
Luca De Nardi

Reputation: 2321

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.

Upvotes: 3

Related Questions