I am using Visual Studio 2015 community edition with AngularJS 1.4.7 in this project.
ProductAPIController.cs
public class ProductAPIController : ApiController
{
[HttpGet]
public IQueryable<Product> MyProducts()
{
List<Product> p = new List<Product>() { new Product {ProductName ="abc", Id = 1, Description = "Desc 1" },
new Product {ProductName ="def", Id = 2, Description = "Desc 2" },
new Product {ProductName ="ghi", Id = 3, Description = "Desc 3" } };
return p.AsQueryable();
}
}
Product.cs
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
}
app.module.js I used a separate file for app module as suggested in best practice here
(function () {
'use strict';
angular.module('app', ['ngCookies', 'ngRoute'])
})();
homeCtrl.js
here I used both service and the controller in the same file. I even tried it using in a separate file, but still it didn't work.
(function () {
'use strict';
angular.module('app')
.controller('homeCtrl', ['$scope', '$http', 'ProductService', '$location', function homeCtrl($scope, $http, ProductService, $location) {
$scope.products = [];
$scope.getProducts = function () {
ProductService.getProducts().then(function (response) {
$scope.products = response.data;
}, function (err) {
alert(err);
});
};
}])
.service('ProductService', ['$http', function ($http) {
this.getProducts = function () {
return $http.get('/api/ProductAPI');
};
}]);
})();
index.cshtml
Here I use data-ng-repeat="item in products"
<div class="row" ng-app="app">
<div class="col-md-12" ng-controller="homeCtrl">
<div class="row">
<div class="col-md-12">
<form id="test">
<div data-ng-repeat="item in products">
<div class="col-sm-4 column productbox">
<div class="producttitle">{{item.ProductName}}</div> <div class="action"></div>
<div class="productprice">
<div class="pull-right">
<div >{{item.Id}}</div>
<a href="" class="btn btn-danger btn-sm" role="button"><span class="glyphicon glyphicon-arrow-right"></span> Get Quote</a>
</div>
<div class="pricetext">{{item.Description}}</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@section scripts
{
@Scripts.Render("~/Scripts/angular.js")
@Scripts.Render("~/Scripts/angular-cookies.min.js")
@Scripts.Render("~/Scripts/angular-route.js")
@Scripts.Render("~/Ng/app.module.js")
@Scripts.Render("~/Ng/homeCtrl.js")
}
I have tried several different approaches but all failed. What am I doing wrong here?
Edit
I have uploaded the sample project to GitHub here . After downloading you may need to rebuild the project to sync related packages.
Edit: Answer
for future reference, in case if an AngularJS beginner like me is looking at this question, the answer as follows. Based on accepted answer given by @Miyuru Ratnayake, just changed the
$scope.getProducts = function () {
......
}
to
function getProducts() {
...
}
and called it before the function declaration. Separate function activate() is not necessary.
so the controller is like this:
.controller('homeCtrl', ['$scope', '$cookies', '$http', 'ProductService', '$location', function homeCtrl($scope, $cookies, $http, ProductService, $location) {
$scope.products = [];
getProducts();
function getProducts() {
ProductService.getProducts().then(function (response) {
$scope.products = response.data;
}, function (err) {
alert(err);
});
};
}])
