将字符串设置为映射到控制器范围成员的指令属性值

Set string as directive attribute value that maps to a member on the controller scope

所以这可能很难解释,但基本上我无法弄清楚如何将对象的字段值评估为指令将用于从范围分配数据的字符串....我有以下模板指令:

<hot-column ng-repeat="column in columns"
     data="{{column.fieldName}}" 
     title="column.title" 
     source="column.lookupField"> // <-- I need this to evaluate to the **"list_currency"** (which I will set on the scope) I cant figure out how this needs to look
</hot-column>

我的列对象是这样的:

[
    {   
        "fieldName": "ccy1",
        "title": "Ccy1",
        "lookupField": "list_currency"
    },
    {
        "fieldName": "ccy2",
        "title": "Ccy2"

    }
]

在控制器中我有这个范围变量:

$scope.list_currency = ["USD", "EUR"];

我已经尝试了一些组合 none 其中有效的是:

source="{{column.lookupField}}" // ==> angular.js:13424 Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{column.lookupField}}] starting at [{{column.lookupField}}].

source="'{{column.lookupField}}'"
source="column.lookupField"
source="'column.lookupField'"

我认为你的问题是你需要 angular 计算表达式两次:

您需要先对 "column.lookupField" 表达式求值,return "list_currency",然后您需要 "list_currency" 表达式求值 return 实际数组。

您应该可以通过向您的控制器添加一个 getColumnSource() 方法来做到这一点 - 就像这样:

$scope.getColumnSource = function(column) {
    return $scope.$eval(column.lookupField);
}

你的 html 看起来像这样:

<hot-column ng-repeat="column in columns"
     data="{{column.fieldName}}" 
     title="column.title" 
     source="getColumnSource(column)">
</hot-column>