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

rex

So this might be hard to explain but basically I cant figure out how to evaluate an object's field value to a string that the directive will use to assign data from the scope.... I have the following template directive:

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

where my column objects are like this:

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

    }
]

And in the controller i have this scope variable:

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

I have tried some combinations none of which work:

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'"
Dan King

I think your problem is that you need angular to evaluate the expression twice:

You need the "column.lookupField" expression to be evaluated first, to return "list_currency", but then you need the "list_currency" expression to be evaluated to return the actual array.

You should be able to do this by adding a getColumnSource() method to your controller - like this:

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

Your html then looks like this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set attribute value of angular directive from another controller

From Dev

Passing attribute value to directive controller

From Dev

angular js directive attribute and controller scope

From Dev

angular js directive attribute and controller scope

From Dev

Get value from directive to $scope in controller

From Dev

Change controller scope value from directive AngularJS

From Dev

How to pass scope value to the directive controller

From Dev

angularjs directive + apply scope value in many controller

From Dev

angularjs controller access directive scope value

From Dev

How to set a default value in an AngularJS Directive Scope?

From Dev

How to set the default value of directive attribute?

From Dev

Directive Scope Boolean Attribute

From Dev

How to set 'inline' controller for $modal in a directive's scope?

From Dev

Evaluate attribute value as number and not string within directive

From Dev

Evaluate attribute value as number and not string within directive

From Dev

scope.$watch withing a directive returns attribute value of undefined

From Dev

Angular JS custom directive - Attribute binding to string or scope variable

From Dev

Change Controller variable value from change in isolate scope directive

From Dev

Accessing directive attribute in controller

From Dev

Access attribute value inside non-directive controller function

From Dev

Getting an attribute value from a directive's isolated controller

From Dev

AngularJS: how to concatenate string with $scope value in a controller

From Dev

assign attribute to $scope in the controller

From Dev

Change a scope value in directive

From Dev

AngularJS initializing $scope of directive/controller

From Dev

Access controller scope from directive

From Dev

Angular scope issue in a Controller and a Directive

From Dev

Update controller scope from directive

From Dev

Binding directive scope with controller in AngularJS

Related Related

  1. 1

    Set attribute value of angular directive from another controller

  2. 2

    Passing attribute value to directive controller

  3. 3

    angular js directive attribute and controller scope

  4. 4

    angular js directive attribute and controller scope

  5. 5

    Get value from directive to $scope in controller

  6. 6

    Change controller scope value from directive AngularJS

  7. 7

    How to pass scope value to the directive controller

  8. 8

    angularjs directive + apply scope value in many controller

  9. 9

    angularjs controller access directive scope value

  10. 10

    How to set a default value in an AngularJS Directive Scope?

  11. 11

    How to set the default value of directive attribute?

  12. 12

    Directive Scope Boolean Attribute

  13. 13

    How to set 'inline' controller for $modal in a directive's scope?

  14. 14

    Evaluate attribute value as number and not string within directive

  15. 15

    Evaluate attribute value as number and not string within directive

  16. 16

    scope.$watch withing a directive returns attribute value of undefined

  17. 17

    Angular JS custom directive - Attribute binding to string or scope variable

  18. 18

    Change Controller variable value from change in isolate scope directive

  19. 19

    Accessing directive attribute in controller

  20. 20

    Access attribute value inside non-directive controller function

  21. 21

    Getting an attribute value from a directive's isolated controller

  22. 22

    AngularJS: how to concatenate string with $scope value in a controller

  23. 23

    assign attribute to $scope in the controller

  24. 24

    Change a scope value in directive

  25. 25

    AngularJS initializing $scope of directive/controller

  26. 26

    Access controller scope from directive

  27. 27

    Angular scope issue in a Controller and a Directive

  28. 28

    Update controller scope from directive

  29. 29

    Binding directive scope with controller in AngularJS

HotTag

Archive