0

I have routing config like following

var app = angular.module("productApp",["ngRoute"]);
app.config(function($routeProvider){
    $routeProvider.when("/",
        {
            templateUrl : "views/home.html",
            controller :  "homeController"
        }).
        when("/add/:productId?",
        {
            templateUrl : "views/products/add.html",
            controller :  "productController",
            resolve : {
                checkId: function($q,$timeout,$http,$route,$location){
                    var defer = $q.defer();
                    var productData = {};
                    var productId = $route.current.params.productId;
                    $http.get("model/checkProductById.php?productId="+productId).then(function(data){
                        if(data.data.status==1){
                            productData = data.data.data;
                            // console.log("afterthis");
                            // console.log(productData);
                            defer.resolve(productData);
                            return productData;
                        }else{
                            //defer.reject();
                            $location.path("/");

                        }
                    });

                    return defer.promise;
                }
            }
        }).
        when("/products",
        {
            templateUrl:    "views/products/view.html",
            controller:     "productController"
        });
});
app.controller("homeController",function($scope){
    $scope.model = {
        message:"Hello World"
    };
});

Check Id in resolve check if product id is valid via $http service.I also checking and returning product object in resolve object.

My controller :

app.controller("productController",function($scope, $http,productService,$routeParams,checkId){

    $scope.productId = $routeParams.productId;
    $scope.model = {
        message:"Hello World from products"
    };
}));

When i pass checkId in controller it giving error of unknown provider. How can i get resolve object in controller.

Thanks

7
  • Hello, you can't inject this specific function in your controller. Why do you need this exactly function in your controller? Commented Oct 2, 2017 at 10:57
  • @L.Figueredo In resolve object i am getting product related data and i have to set that data in view after resolve. I can get data again by calling $http again. But i want to get product data at the same time of resolving.Is there is any way? Commented Oct 2, 2017 at 11:00
  • When i console.log($scope). I can see the value in $scope.$resolve.checkId. But when i call it throw error Commented Oct 2, 2017 at 11:02
  • You can see if this resolve your situation: johnpapa.net/route-resolve-and-controller-activate-in-angularjs - I think this explains about what you need. Commented Oct 2, 2017 at 11:07
  • Thanks : I am also i think following second route approach. Instead of creating service i am directly defining it. Let see what happen when i create service Commented Oct 2, 2017 at 11:16

1 Answer 1

2

Finally get the data via $route with isDefined condition

app.controller("productController",function($scope, $http,productService,$routeParams,$route){

        $scope.productId = $routeParams.productId;
        $scope.model = {
            message:"Hello World from products"
        };
        if(angular.isDefined($route.current)){
            console.log($route.current.locals.checkId);
        }
        });
Sign up to request clarification or add additional context in comments.

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.