0

I have written the following directive:

var gameOdds = function(){
    return {
        template: '{{games["@homeTeam"]}} vs {{games["@awayTeam"]}}',
        scope: {
            games: '@'
        }
    };
};

<div game-odds games="{{games}}">

This uses the following JSON data (part of the json is below):

{
    @id: "69486",
    @homeTeam: "Home Team",
    @awayTeam: "Away Team",
    otherNormalValues : {
        etc: "normal..."
    }
}

I know that the method of selecting these keys preceded with an @ symbol works when put directly into the HTML bound to a controller. But in my directive I cannot select the fields in this ["@field"] way.

Does anyone know how to do this?

2
  • is games an array of objects? Suggest you create a simple demo with real data and controller. Not sure why you have @ prefix for all your properties Commented Sep 12, 2014 at 11:16
  • Games contains the JSON shown. I don't know why the JSON returns @ symbols, it is a third party API. Commented Sep 12, 2014 at 11:36

2 Answers 2

2

Instead of using the attribute object notation, @, you can use the = instead.

DEMO

JAVASCRIPT

  .directive('gameOdds', function() {
    return {
      template: '{{games.homeTeam}} vs {{games.awayTeam}}',
      scope: {
        games: '='
      }
    }
  });

HTML

<div game-odds games="games"></div>

Update: Sorry for the late reply, as what the accepted answer had mentioned, you can access them with the [] notation, if the key starts with special characters in it:

  .directive('gameOdds', function() {
    return {
      template: '{{games['@homeTeam']}} vs {{games['@awayTeam']}}',
      scope: {
        games: '='
      }
    }
  });
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't solve the problem for me. I cannot reference games.awayTeam because it contains an @ symbol. games.awayTeam would return undefined. games["@awayTeam"] returns the team name.
IT's the same, just replace his template with yours. The key point is to use = and pass games instead of {{games}}. With @ you are passing text instead of a scope variable
With = I just receive the actual code representation of the object?
sorry for the late reply, I lost my internet connection earlier, I have updated my answer. But I guess it has already been answered, so I modified this answer as well.
1

The @ symbol on scope transforms whatever you pass to the attribute games into text, and passes it into your directive. If you use the = symbol, you can pass a scope variable into the directive.

With @, scope.games will be a string

With =, scope.games will be your json object

var gameOdds = function(){
    return {
        template: '{{games["@homeTeam"]}} vs {{games["@awayTeam"]}}',
        scope: {
            games: '='
        }
    };
};

<div game-odds games="games">

1 Comment

This is correct, I had games="{{games}}" rather than games="games", which meant it was just dumping the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.