3

products_controller.js

    var myApp1 = angular.module('myapplication', ['ngRoute', 'ngResource']); 

//Factory
myApp1.factory('Products', ['$resource',function($resource){
  return $resource('/products.json', {},{
    query: { method: 'GET', isArray: true },
    create: { method: 'POST' }
  })
}]);

myApp1.factory('Product', ['$resource', function($resource){
  return $resource('/products/:id.json', {}, {
    show: { method: 'GET' },
    update: { method: 'PUT', params: {id: '@id'} },
    delete: { method: 'DELETE', params: {id: '@id'} }
  });
}]);

//Controller
myApp1.controller("ProductListCtr", ['$scope', '$http', '$resource', 'Products', 'Product', '$location', function($scope, $http, $resource, Products, Product, $location) {

  $scope.products = Products.query();

  $scope.deleteProduct = function (productId) {
    if (confirm("Are you sure you want to delete this product?")){
      Product.delete({ id: productId }, function(){
        $scope.products = Products.query();
        $location.path('/');
      });
    }
  };
}]);

myApp1.controller("ProductUpdateCtr", ['$scope', '$resource', 'Product', '$location', '$routeParams', function($scope, $resource, Product, $location, $routeParams) {
  $scope.product = Product.get({id: $routeParams.id})
  $scope.update = function(){
    if ($scope.productForm.$valid){
      Product.update({id: $scope.product.id},{product: $scope.product},function(){
        $location.path('/');
      }, function(error) {
        console.log(error)
      });
    }
  };

}]);

myApp1.controller("ProductAddCtr", ['$scope', '$resource', 'Products', '$location', function($scope, $resource, Products, $location) {
  $scope.product = {:name, :price, :description }]}
  $scope.save = function () {
    if ($scope.productForm.$valid){
      Products.create({product: $scope.product}, function(){
        $location.path('/');
      }, function(error){
        console.log(error)
      });
    }
  }
}]);



//Routes
myApp1.config([
  '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/products',{
      templateUrl: '/templates/products/index.html',
      controller: 'ProductListCtr'
    });
    $routeProvider.when('/products/new', {
      templateUrl: '/templates/products/new.html',
      controller: 'ProductAddCtr'
    });
    $routeProvider.when('/products/:id/edit', {
      templateUrl: '/templates/products/edit.html',
      controller: "ProductUpdateCtr"
    });
    $routeProvider.otherwise({
      redirectTo: '/products'
    });
  }
]);

templates/products/index.html

<br/>
<div class="row">
  <div class="col-md-12">
    <a class="btn btn-primary" href="#/products/new">Create a product</a>
    <h3 class="block">Products</h3>
    <table class="table table-striped">
      <tr>
        <th>Name</th>
        <th>Price</th>
        <th>Description</th>
        <th></th>
      </tr>
      <tr ng-hide="products.length" >
        <td colspan="5">No products found, Please create one.</td>
      </tr>
      <tr ng-show="products.length" ng-repeat="product in products">
        <td>{{product.name}}</td>
        <td>{{product.price}}</td>
        <td>{{product.description}}</td>

        <td>
            <a href="#/products/{{product.id}}/edit">Edit</a> | <a href="" ng-click="deleteProduct(product.id)">Remove</a>
        </td>
      </tr>
    </table>
  </div>
</div>

*When I go to the Url: localhost:3000/products, it shows blank page. I have added //= require angularjs-rails in application.js . I am new in angular-js. Please help me. *

2
  • Any errors showing in the console? Commented Apr 10, 2015 at 10:34
  • I noticed you are calling query as a function: $scope.products = Products.query(); when query is an object? Commented Apr 10, 2015 at 13:24

1 Answer 1

2

you just give the path for view like as:

var myApp = angular.module('myapplication', ['ngRoute', 'ngResource']); 

//Factory
myApp.factory('Products', ['$resource',function($resource){
  return $resource('/products.json', {},{
    query: { method: 'GET', isArray: true },
    create: { method: 'POST' }
  })
}]);

myApp.factory('Product', ['$resource', function($resource){
  return $resource('/products/:id.json', {}, {
    show: { method: 'GET' },
    update: { method: 'PUT', params: {id: '@id'} },
    delete: { method: 'DELETE', params: {id: '@id'} }
  });
}]);

//Controller
myApp.controller("ProductListCtr", ['$scope', '$http', '$resource', 'Products', 'Product', '$location', function($scope, $http, $resource, Products, Product, $location) {

  $scope.products = Products.query();

  $scope.deleteProduct = function (productId) {
    if (confirm("Are you sure you want to delete this product?")){
      Product.delete({ id: productId }, function(){
        $scope.products = Products.query();
        $location.path('/#/products');
      });
    }
  };
}]);

myApp.controller("ProductUpdateCtr", ['$scope', '$resource', 'Product', '$location', '$routeParams', function($scope, $resource, Product, $location, $routeParams) {
  $scope.product = Product.get({id: $routeParams.id})
  console.log(Product.get({id: $routeParams.id}));
  $scope.update = function(){
    if ($scope.productForm.$valid){
      Product.update({id: $scope.product.id},{product: $scope.product},function(){
        $location.path('/products');
      }, function(error) {
        console.log(error)
      });
    }
  };

}]);

myApp.controller("ProductAddCtr", ['$scope', '$resource', 'Products', '$location', function($scope, $resource, Products, $location) {
  $scope.product = {name}
  $scope.save = function () {
    if ($scope.productForm.$valid){
      debugger
      Products.create({product: $scope.product}, function(){
        $location.path('/products')
      }, function(error){
        console.log(error)
      });
    }
  }

}]);



Routes
 myApp.config([
   '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
     $routeProvider.when('/products',{
       templateUrl: '/templates/products/index.html',
       controller: 'ProductListCtr'
     });
     $routeProvider.when('/products/new', {
       templateUrl: '/templates/products/new.html',
       controller: 'ProductAddCtr'
     });
     $routeProvider.when('/products/:id/edit', {
       templateUrl: '/templates/products/edit.html',
       controller: "ProductUpdateCtr"
     });
      $routeProvider.otherwise({
        redirectTo: '/products'
     });
   }
 ]);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain why you have written this line "$scope.product = {name}" ?
scope is an object that refers to the application model. It is an execution context for expressions. so in this you are trying to access product

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.