I am writing a simple UI for a product query. On load the page reveals a list of products from a RESTful API call. It uses ng-repeat to decorate the data and render it in a list. At rendering time, the app checks for mobile. The requirement is that if the user is on a desktop, it will open in a new window. If mobile, new tab. (Please don't debate the merits of new window. That is not negotiable.)
What I need is something to tell the app, when the user clicks on a product for the detail view, whether to render either a new tab or new window.
My questions is whether or not this belongs in a directive? If so, can you describe to me roughly how that would flow?
I'm including the code below. Let me know if you need more code/info to help answer the question.
HTML
<section>
<div class="headerWrap box-shadow">
<h2 class="box-shadow">Product Selection</h2>
</div>
<ul class="products">
<li ng-repeat="product in products" class="productPane box-shadow">
<a ng-show="isMobile" ng-href="#/products/{{product.product_id}}" ng-click="getProduct('mobile')">
<div class="productMeta box-shadow">
<img ng-src="{{product.image}}">
<h3>{{product.name}}</h3>
<p>{{(product.price/100) | currency: "€" : 2}}</p>
</div>
</a>
<a ng-show="!isMobile" ng-click="getProduct('notMobile')">
<div class="productMeta box-shadow">
<img ng-src="{{product.image}}">
<h3>{{product.name}}</h3>
<p>{{(product.price/100) | currency: "€" : 2}}</p>
</div>
</a>
</li>
</ul>
</section>
Controllers
var DemoControllers = angular.module('DemoControllers', []);
DemoControllers.controller('ProductListCtrl', ['$scope', '$http', 'list',
function ($scope, $http, list) {
list.getList().success(function (data, status, headers, config) {
$scope.products = data.products;
});
}]);
DemoControllers.controller('ProductDetailCtrl', ['$scope', '$http', '$routeParams', 'product',
function ($scope, $http, $routeParams, product) {
$scope.product_id = $routeParams.product_id;
product.getProduct().success(function (data, status, headers, config) {
$scope.selected = data;
};
}]);
Services
var DemoServices = angular.module('DemoServices', []);
DemoServices.factory('list', ['$http', function ($http) {
return {
getList: function () {
return $http.get('https://s3-eu-west-1.amazonaws.com/developer-application-test/cart/list');
}
}
}]);
DemoServices.factory('product', ['$http', function ($http, id) {
return {
getProduct: function ($http, id) {
return $http.get('https://s3-eu-west-1.amazonaws.com/developer-application-test/cart/' + id + '/detail');
}
}
}]);