最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - In Angular JS, how do I inject data from directive attribute to template? - Stack Overflow

matteradmin6PV0评论

Here's my directive:

app.directive("helloWorld", function() {
  return {
    restrict: "E",
    scope: {
      name: "bind"
    },
    template: "<div>a {{name}} a</div>"
  };
});

Here's how I use it:

<hello-world name="John Smith"></hello-world>

I expect this page to be like this, when I run it:

<hello-world>
  <div>a John Smith a</div>
</hello-world>

But for some reason, name is not injected and actual result is like this:

<hello-world>
  <div>a {{name}} a</div>
</hello-world>

Anything I'm missing? I'm using Angular JS 1.0.2

Here's my directive:

app.directive("helloWorld", function() {
  return {
    restrict: "E",
    scope: {
      name: "bind"
    },
    template: "<div>a {{name}} a</div>"
  };
});

Here's how I use it:

<hello-world name="John Smith"></hello-world>

I expect this page to be like this, when I run it:

<hello-world>
  <div>a John Smith a</div>
</hello-world>

But for some reason, name is not injected and actual result is like this:

<hello-world>
  <div>a {{name}} a</div>
</hello-world>

Anything I'm missing? I'm using Angular JS 1.0.2

Share Improve this question asked Oct 14, 2012 at 10:20 Andrey AgibalovAndrey Agibalov 7,6748 gold badges67 silver badges112 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

The scope declaration is strange. I'm not sure about the "bind" declaration - maybe its something from the previous versions.

The current syntax for binding to a directive's attribute is like this:

return {
    restrict: "E",
    scope: {
      name: "@name"
    },
    template: "<div>a {{name}} a</div>"
};

In general, @attributeName. See here for more information on directives.

Post a comment

comment list (0)

  1. No comments so far