0

cartController in AngularJS:

angular.module('demo', [])
.controller('Hello', function($scope, $http) {
    $scope.refreshCart = function() {
    $http.get('http://localhost:8080/rest/cart')
        .success(function(response) {
            $scope.items = response.data;
        });
};
    $scope.removeFromCart = function(productId) {
        $http.delete('/delete/' + productId)
            .success(function (data) {
                $scope.refreshCart();
            });
    };
    $scope.addToCart = function(productId) {
        $http.put('/add/'+ productId)
            .then(function(response) {
                $scope.refreshCart();
        });
    };
});

First HTML file (here everything works):

<a href = "#" type="button" class="btn btn-info" th:attr="
                       ng-click='addToCart(' + ${product.id} + ')'" data-toggle="modal" data-target="#myModal">
                            Add to cart</a>

Second HTML file:

    <html lang="en" xmlns:th="http://www.thymeleaf.org" ng-app="demo">
<script src="http://localhost:8080/cartController.js"></script>
<body ng-controller="Hello">
    (...)
 <tbody ng-repeat="item in items.cartItemList">
            <div class="row">
                    <h4 class="nomargin">{{item.product.name}}</h4>
                    <p>{{item.product.description}}</p>
            </div>
        <td data-th="Price">{{item.price}} PLN</td>
        <td data-th="Quantity">{{item.quantity}}</td>
        </tbody>
(...)

So what i need to do is:

1) Hit the button in first HTML file, and load JSON to $scope.items (it works).

2) Show the second HTML file, load JSON from $scope.items, and view this JSON to user.

But when I get the second HTML file and try to show data, the $scope.items is empty. Can you help pleae ?

3
  • Why does your "second" HTML file contains a html tag + ng-appattribute? Do you know how SPA works? Commented Mar 4, 2018 at 19:58
  • Hmm should I delete it ? SPA ? Never heard; < Commented Mar 4, 2018 at 20:33
  • I removed ng-app attribute but still doesn't work properly, Commented Mar 4, 2018 at 20:42

2 Answers 2

1

Do you get console errors in your browser? Maybe you have to define items on the controller as empty array like in the example below ...

.controller('Hello', function($scope, $http) {
    //define empty array
    $scope.items = [];

    $scope.refreshCart = function() {
        $http.get('http://localhost:8080/rest/cart')
        .success(function(response) {
            $scope.items = response.data;
        });
    };
    //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

No, I don' get any errors. I tried your solution, but unfortunately it doesn't work. I suspect the error might be in ng-app="demo" (in both files).
0

I would suggest you to use broadcast and emit. Pass data between the controllers using these. You should use $broadcast if you want to pass data from parent controller to child controller. And emit if the other way around.

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.