javascript - 根据传递的 Angular 值动态设置样式

标签 javascript jquery css angularjs

我有一个输入值,我想根据从 Angular 变量接收到的值设置样式。所以在我的 cshtml 中,我有:

 <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 >

然后在我的 Angular .js 文件中有以下内容:

 $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.");
    });

根据我收到的返回值(“radio”或“text”),我想要不同的样式。例如,我希望单选按钮的高度为 20px,文本必须为 40px。我将如何根据从“input_type”收到的文本值对我的输入值进行条件样式设置。

如有任何帮助,我们将不胜感激。

最佳答案

语法是

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

因此,您首先指定要设置的属性,然后根据您的逻辑指定其值。

关于javascript - 根据传递的 Angular 值动态设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092650/

相关文章:

javascript - 带有位置服务的angularjs注入(inject)器

javascript - 在索引 N 之后使用 jQuery 删除 div

Javascript 变量不会更新番茄时钟

javascript - 这个 if/else Javascript 函数有什么问题?

html - 正方形中的水平线和垂直线

css - 编写有效的 XHTML 文本区域?不能省略行和列?

javascript - 在 Google map 上使用 KML 叠加层进行用户输入搜索

javascript - 访问控制允许来源错误

javascript - 如何覆盖 .js 文件内容

html - Firefox和Chrome之间的flex-shrink差异