简体   繁体   English

AngularJS在模型中更改属性名称

[英]AngularJS changing property name in model

The data I am receiving is structured like this. 我接收的数据的结构如下。 I am taking this data and displaying it on to a graph with radio buttons. 我正在获取这些数据,并将其显示在带有单选按钮的图形上。 Each radio button will show the "category name", but actually I need to show a different label for the category name other than what is coming back in the data. 每个单选按钮将显示“类别名称”,但实际上我需要为类别名称显示一个不同的标签,而不是返回数据中的内容。 For example if the category name is false, I need the radio button to display: "Other" and if it is true I need to display: "Our Company". 例如,如果类别名称为false,则需要单选按钮显示:“其他”;如果为true,则需要显示:“我们的公司”。

0: Object
    categoryName: false
    categories: Array[4]
        0: Object
        1: Object
        2: Object
        3: Object
1: Object
    categoryName: true
    categories: Array[2]
        0: Object
        1: Object

<span ng-repeat="testCat in testCategories">
<input type="radio" name="programSelector2" ng-model="testCategoryId" ng-click="load( testCat.id )" value="{{testCat.id}}" >{{testCat.name}}
</span>

Need some help with controller logic I'm assuming to update the model based on what the categoryName is. 我需要控制器逻辑方面的帮助,我假设要根据categoryName是什么来更新模型。

You can follow three different roads: 您可以遵循三条不同的道路:

  • create a function to resolve the value 创建一个函数来解析值
  • create an array to solve the value 创建一个数组来求解值
  • use a condition in the expression 在表达式中使用条件

With resolve function: 具有解析功能:

$scope.resolve = function(key) {
    if (key == 'firstValue') {
        return 'First description';
    } else if (key == 'secondValue') {
        return 'Second description;
    }
}

and in the HTML 并在HTML中

{{resolve(testCat.categoryName)}}  

With array 带阵列

$scope.resolve = [];
$scope.resolve['firstValue'] = 'First description;
$scope.resolve['secondValue'] = 'Second description;

and in the HTML 并在HTML中

{{resolve[testCat.categoryName]}}  

With conditional expression 有条件表达

Or in the case that categoryName has only true false values 或在categoryName只有真假值的情况下

 {{ testCat.categoryName ? 'First description' : 'Second description' }}

Create function in the controller like : 在控制器中创建函数,如:

scope.getCategoryName=function(categoryName){
   if(categoryName === true)
     return 'Other';
   else 
   {
     ...
   }
}

and then call it from your html 然后从您的html调用它

<input type="radio" name="programSelector2" ng-model="testCategoryId" ng-click="load( testCat.id )" value="{{testCat.id}}" >{{getCategoryName(testCat.name)}}

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

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