var arr = window.location.href.split("/"); if (arr[3].indexOf('app') == 0) baseUrl = ''; else baseUrl = '/' + arr[3]; angular.module('CarbonFootprint').config( function ($routeProvider) { $routeProvider.when('/Product/Edit/:productId', { templateUrl: baseUrl+'/Partials/Product-Create', controller: 'ProductEditController' }); } ) .controller('ProductEditController', [ '$scope', '$routeParams', '$http', '$location', '$timeout', function ($scope, $routeParams, $http, $location, $timeout) { $scope.product = { }; // set $scope.baseUrl var baseUrl = $scope.baseUrl; if (typeof (baseUrl) === 'undefined' || baseUrl == null) { var arr = window.location.href.split("/"); if (arr[3].indexOf('app') == 0) baseUrl = ''; else baseUrl = '/' + arr[3]; $scope.baseUrl = baseUrl; } $http.get($scope.baseUrl+'/api/Product/Get/' + $routeParams.productId) .success(function (data, status, headers, config) { $scope.product = data; }); $scope.saveProduct = function () { if ($scope.form.$invalid) return; //console.log('save product', $scope.product); $http.post($scope.baseUrl+'/api/Product/Save', $scope.product) .success(function (data, status, headers, config) { $location.path('/Product/Index'); }) .error(function (data, status, headers, config) { if (data?.Message != undefined) alert(data.Message) else alert('Oops! This is something happen. Maybe you can try it later'); }); }; $scope.cancel = function () { $location.path('/Product/Index'); }; $scope.uploadFile = function () { $('#product-photo').click(); }; $scope.selectPhoto = function (photo) { var formData = new FormData(); formData.append('file', photo); $http.post($scope.baseUrl+'/api/Upload', formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }) .success(function (data) { $scope.product.PhotoUrl = $scope.baseUrl + data[0]; //console.log(' product PhotoUrl', $scope.product.PhotoUrl); }) .error(function () { console.log('500 error'); }); }; } ]);