3

I'm new to AngularJS and stuck on below code.

app.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: "partials/home.html",
        controller: "mainController",
    })
    .when('/products', {
        templateUrl: "partials/productlist.html",
        //controller: "ProductController",
    })
    .when('/product/:prodID', {
        templateUrl: "partials/product.html",
        controller: "viewController",
    })
    .when('/contact', {
        templateUrl: "partials/contact.html",
        controller: "contactController",
    })
    .otherwise({
        redirectTo: "/"
    });
});

app.controller('ProductController', function($scope, $http){
    $http.get('partials/productTable.json').success(function(response){
        $scope.datap = response.lists;
    });
 }).
controller('viewController',function($scope,$routeParams){
    $scope.eachproduct = $scope.datap[$routeParams.prodID];
});

And my product.html page code will look like this.

<div ng-controller="viewController">
    <ol class="breadcrumb">
        <li><a href="#">Home</a></li>
        <li><a href="#">Products</a></li>
        <li class="active">{{eachproduct.link}}</li>
    </ol>
    <div class="col-md-4">
        <figure><img ng-src="{{ }}"></figure>
        <p>
            <a href="">Read More</a>
        </p>
    </div>
</div>

Problem is when I navigate to any product page value of {{eachproduct.link}} is not showing.

Any solution will be appriciated.

5
  • you commented out the ProductController within .when('/products' ? Commented Sep 25, 2015 at 4:43
  • If I remove comment then the ProductController is not working :( Commented Sep 25, 2015 at 4:46
  • what are the errors you are getting Commented Sep 25, 2015 at 4:46
  • @HieuLe I removed the commented part now productlist.html page is working fine but problem is still there with product.html page. Should I add $rootScope? Commented Sep 25, 2015 at 4:52
  • this is because when you go to a product page, it does NOT run the productcontroller code. It will only run the viewController Commented Sep 25, 2015 at 4:53

3 Answers 3

3

Use $rootScope instead of $scope

$rootScope

The $rootScope is the top-most scope. An app can have only one $rootScope which will be shared among all the components of an app. Hence it acts like a global variable. All other $scopes are children of the $rootScope.

Sample :

    controller('viewController',['$scope','$routeParams', '$http','$rootScope',function($scope,$routeParams, $http,$rootScope){
    $http.get('partials/productTable.json').success(function(response){
        $scope.datap = response.lists;
       $rootScope.eachproduct = $scope.datap[$routeParams.prodID];
     });
   }]);
Sign up to request clarification or add additional context in comments.

10 Comments

But where should I pass the $rootScope in above example? Is it something like below?
app.controller('ProductController', function($scope, $http, $rootScope){ $http.get('partials/productTable.json').success(function(response){ $scope.datap = response.lists; }); }). controller('viewController',function($scope,$routeParams){ $rootScope.eachproduct = $scope.datap[$routeParams.prodID]; });
@VANKD Yes like that only.
Creating service would be better
@PankajParkar is it possible for you to provide any example with my code?
|
0
app.controller('ProductController', function($scope, $http){
    $http.get('partials/productTable.json').success(function(response){
        $scope.datap = response.lists;
    });
 }).
controller('viewController',function($scope,$routeParams, $http){
    $http.get('partials/productTable.json').success(function(response){
        $scope.datap = response.lists;
        $scope.eachproduct = $scope.datap[$routeParams.prodID];
    });
});

1 Comment

"ReferenceError: $http is not defined" and product.html still showing me {{eachproduct.link}}
0

It seems like what you are looking for is an angular provider such as a factory to store the values in, this will allow the values to be pass values around the controllers while using the routes.

Have a look at this example, while it isn't using routes, the principal is the same: https://jsbin.com/wiwejapiku/edit?html,js,output

For more information on providers have a look here: https://docs.angularjs.org/guide/providers

Your example would work something like this:

app
.factory('productFactory',function(){
    return {
        data: {}
    };
})
.controller('ProductController', function($scope, $http, productFactory){
    $scope.productFactory = productFactory;
    $http.get('partials/productTable.json').success(function(response){
         $scope.productFactory.data = response.lists;
    });
}).
controller('viewController',function($scope,$routeParams, productFactory){
    $scope.productFactory = productFactory;
    $scope.eachproduct = $scope.productFactory.data[$routeParams.prodID];
});

Note you would also have to change your view to reference 'productFactory.data' respectively.

5 Comments

kind of new to this myself. Will every call to productFactory keep calling the code to get the data? or will it only do it once? For example, if there is an ajax call to get the data, then will that get called every time it is injected to the controller?
No, the call will only happen once, the purpose of providers in angular is to allow you to have a persistant state throughout the application. So any changes you make to the factory would mean that they will be accessible from anywhere else that they are consumed. The data will only get changed if you change it (either manually or requesting another ajax call).
@benfes I will also try with factory provider for my learning thanks for the help.
@benfes how should I change my view? I used your solution but noting is showing on screen. And also no errors in Console.
You shouldn't need to refactor your view, are you able to post up your code with the changes? Or provide an example on codepen or jsbin?

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.