0

I have an array like below;

$scope.cart = [];

This contains items like id, name etc.

How can I include the items in a href like below;

<a href="http://www.example.co.uk/search-menu/{{$scope.cart.restaurant_id}}/test">

This doesn't work.

I've also tried using ng-repeat=item in cart

FYI When I console.log($scope.cart) I get the below;

[Object]
0: Object
cookie_id: "j0m3hdkf4c371nueajdi9gf4f3"
date: "0000-00-00"
day: ""
id: "92"
location: ""
restaurant_id: "1"
time: "00:00:00"
user_id: "63"
1
  • Expression does not need $scope Commented Nov 24, 2015 at 14:08

3 Answers 3

4

Its Magic in Two way Data binding

Use ng-href directive in angularjs

Remove $scope in the anchor tag. You directly access the scope variable in the view part. In the controller part only we use $scope object is not need for html view.

<a ng-href="http://example.co.uk/search-menu{{cart[0].restaurant_id}}/test">
Sign up to request clarification or add additional context in comments.

5 Comments

Again you mistake you ng-repeat with item but you used cart in anchor tag
use item instead of cart in the anchor tag. your code works beautiful
I'm no longer using ng-repeat based on this answer
it you not used ng-repeat then get index by zero
try <a ng-href="example.co.uk/search-menu{{cart[0].restaurant_id}}/test">
0

ng-repeat should work actually. But you should try it with ng-href, like below...

In your HTML:

ng-repeat="item in cart" ng-href="yourUrl/{{item.id}}"

In your controller:

$scope.cart = [ cart1, cart2, ...];

If you get any errors, i would really help to see them here!

Comments

0

First thing i would do is to remove the $scope in your controller and replace it with this

this.cart = [];

Then in your View you can use Controller As declaration then you will end up with dot notation (Which is the better way of doing it)

for example

    <div data-ng-controller="CartController as cart">
        <a ng-href="http://www.example.co.uk/search-menu/{{cart[0].restaurant_id}}/test">
    </div>

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.