0

I have a set up where I am trying to use my JSON object to create the URL strings in an ng-repeat. The text is viewable when inspected in the broswer, yet it doesn't work, or console log an error.

The HTML looks like this:

<div ng-repeat="project in projects">
  <div ng-click="go('/projects/{{project.url}}')"></div>
</div>

I parse the $location service in to my controller, and then use the following to change the page url (please note, I have checked this works using non-dynamic URLs in the view):

$scope.go = function (path) {
  $location.path(path);
};

An example of the JSON object I am using, looks like this:

$scope.projects = [
  {
    url: 'paul-davis'
  }
];
4
  • Remove the curly braces from project.url -- you're already writing in angular code within an ng-click (I think anyways, removed the answer until I'm sure) Commented Oct 31, 2013 at 13:22
  • Sorry, that just puts 'project.url' at the end of the URL string. Commented Oct 31, 2013 at 13:24
  • Weird...you shouldn't need to use {{}}...waaait, try this (removing some quotes) :: ng-click="go('/projects/'project.url)" Commented Oct 31, 2013 at 13:28
  • Does the method go get called? Commented Oct 31, 2013 at 13:32

2 Answers 2

4

The code within ng-click is angular script, not template code to use {{...}}. Use it as:

<div ng-click="go('/projects/' + project.url)"></div>
Sign up to request clarification or add additional context in comments.

2 Comments

what about when you have the ng-click that accesses a method in the controller like <div ng-click="doMethod({{ myObject.id}})" >asdfasdf<div>? I just cannot get this to work... its driving me nuts!
for anyone else... you dont need to use {{var}} inside of the ng-click
0

You can put an <a> tag with dynamic href inside of the ng-repeat, it works just as you'd expect - though using ng-href is better, so your links dont break before the data bindings are ready. you can have a look to this Angular tuto https://docs.angularjs.org/api/ng/directive/ngHref

Comments

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.