Call mvc url.action from angular controller - angularjs

I have this tabset:
<tabset justified="true" class="tabsetnowrap">
<tab ng-repeat="tab in tabshomepage track by $index" heading="{{tab.title}}" ng-click="homePageNavigate(tab.type)" active="tab.active" disabled="tab.disabled">
</tab>
It is created in a angular controller:
$scope.tabshomepage = [];
$scope.tabshomepage.push(
{ title: 'Hjem', type: 'HOM', order: 1, active: true, disabled: false, color: 'black' },
{ title: 'Dirty', type: 'DIR', order: 2, active: false, disabled: false, color: 'purple' },
{ title: 'Dating', type: 'DAT', order: 3, active: false, disabled: false, color: 'green' },
{ title: 'Bloggers', type: 'SOC', order: 4, active: false, disabled: false, color: 'lblue' },
{ title: 'Konto', type: 'ACO', order: 5, active: false, disabled: false, color: 'black' },
{ title: 'Om os', type: 'ABU', order: 6, active: false, disabled: false, color: 'black' },
{ title: 'Kontakt og FAQ', type: 'COF', order: 7, active: false, disabled: false, color: 'black' }
);
When a click is done on a tab, then the homePageNavigate function is performed.
$scope.homePageNavigate = function (type) {
if(type == 'DIR'){
//Perform a #Url.Action("Index", "Dirty", null)"
}
etc...
};
In that functin I want to call a mvc method: #Url.Action("Index", "Dirty", null)", and return a view("index")
What is best way to solve this problem?
Any workarounds? Or a simple solution to this?

I've done something similar before by performing a bit of configuration in angular, through razor, on page load:
<script>
(function () {
angular.module('App').value('paths', {
home: '#Url.Action("Index", "Dirty", null)'
// more paths here
});
})();
</script>
Then you can inject and use paths anywhere within your angular app.
eg. Inside a controller called 'myCtrl`
angular.module('App').controller('myCtrl', ['paths', function(paths) {
// you can use paths.home here
}]);

It's quite simple to invoke ASP.Net MVC Controller action method using AngularJS as follows
In Javascript I wrote
var app = angular.module('MyApp', []);
angular.module('MyApp', [])
.controller('MyController', ['$scope', '$http', function ($scope,
$http) {
$scope.search = function () {
$http({
method: "GET",
url: "/home/GetData"
}).then(function mySuccess(response) {
$scope.name = response.data;
}, function myError(response) {
$scope.name = response.statusText;
});
}
}]);
In HTML part, I wrote
<button ng-click="search()">Get Data</button>
In Controller Action, I wrote return Json("You caught AngularJS", JsonRequestBehavior.AllowGet);

Related

Highstock Navigator Displayed Twice

I am using highstock,5.0.10 with angularJS.
In my chart navigator got displayed twice, like below
As you can see, navigator has been rendered twice. However, if you refresh the page, everything looks fine.
Any ideas?
I have added my Highstock code here.
Highcharts.stockChart('FindComputerUsage', {
rangeSelector: {
selected: 1
},
credits: {
enabled: false
},
title: {
text: ''
},
yAxis: {
title: {
text: 'Percentage of time (%) '
}
},
tooltip: {
formatter: function() {
var date = new Date(requiredData[this.points[0].point.index][0]);
return date + '<br>' + 'Percentage:' + requiredData[this.points[0].point.index][1] + '%';
}
},
series: [{
name: 'Percentage (%)',
data: requiredData,
tooltip: {
valueDecimals: 2,
}
}],
lang :{
noData : "No data to display "
}
});
Here you can see a working example: https://plnkr.co/edit/OphM9wf8WyGm8U6HXfTy
You haven't show your HTML view, but I guess it's similar to this.
<div ng-controller="demoCtrl">
<div id="container" style="height: 400px; min-width: 310px"></div>
</div>
Concerning controller scope, I didn't change anything:
var app = angular.module('demoApp', []);
app.controller('demoCtrl', function($scope){
$scope.data = [
[1290729600000,45.00],
[1290988800000,45.27],
[1291075200000,44.45]
];
Highcharts.stockChart('container', {
rangeSelector: {
selected: 1
},
title: {
text: 'Qing Xu Example'
},
series: [{
name: 'Value',
data: $scope.data,
tooltip: {
valueDecimals: 2
}
}]
})
});

ng-click not working with Datatable and Angular Js

I have read several similar questions but none worked for me on this issue. I am using laravel-datatable with angular js. But 'ng-click' does not work with the dynamically generated datatable buttons. I read about $compile but dont know how to implement it with the table. Am very new to angular js.
When I click on the button nothing happens.
app.controller('myController', ['$scope','$compile', function($scope, $compile) {
$('#stafftbl').DataTable( {
paging: true,
"ordering": true,
"searching": true,
"bInfo" : true,
deferRender: true,
ajax: 'get_staffsalary',
columns: [
{ data: 'staff_num', name: 'staff_num'},
{ data: 'staffname ', name: 'staffname' },
{ data: 'salary', name: 'salary' },
{ data: 'date_effected', name: 'date_effected' },
{ data: 'updated_at', name: 'updated_at' },
{ data: null, render: function (data, type, full) {
return '<button class="btn btn-xs btn-primary" ng-click="update('+full.id+')">Update Salary</button> ';
}},
],
});
$scope.update= function (id) {
alert(id)
}
}]);
Please any help with this?
Yes, you are correct in that you need to use $compile.
After dynamically adding html with angular attributes in it, like you are with your data table, you must call $compile so that angular can pick up the attributes.
You'll need to inject the $compile service into your controller right after $scope. Then, after the HTML has been added you will need to compile the new DOM and link it to the scope by calling $compile(new_html)($scope) in the context of your controller.
Refer to the $compile doco for reference
I solved it using #MJL 's answer. Here is the full block of code, it may hep someone else
app.controller('myCtrl', ['$scope','$compile', function($scope, $compile) {
var table = $('#stafftbl').DataTable( {
processing: false,
serverSide: false,
deferRender: true,
ajax: 'get_staffsalary',
columns: [
{ data: 'staff_num', name: 'staff_num'},
{ data: 'salary', name: 'salary' },
{ data: 'date_effected', name: 'date_effected' },
{ data: 'updated_at', name: 'updated_at' },
{ data: null, render: function (data, type, full) {
return '<button class="btn btn-xs btn-primary text-size-small" ng-click="clickme()>Update</button> ';
}},
],
"createdRow": function ( row, data, index ) {
$compile(row)($scope); //add this to compile the DOM
}
})
$scope.clickme = function () {
alert('clicked me')
}
}]);

I get buttons instead of radio buttons?

I'm working on a project with angular JS and i need to make a list of radio buttons to allow the user change the basemap. But i get the list without the circle of the radio button. My controller is like so:
(function() {
'use strict';
angular
.module('gdrtApp')
.controller('MapController', MapController);
MapController.$inject = ['$scope', '$http', 'olData'];
function MapController ($scope, $http, olData) {
angular.extend($scope, {
center: {
lat: 47.383474,
lon: 1.329346,
zoom: 6,
autodiscover: true
},
layers: [
{
name: 'OpenStreetMap',
active: true,
source: {
type: 'OSM',
url: "http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution: '© OpenStreetMap contributors'
}
},
{
name: 'Stamen',
active: false,
source: {
type: 'Stamen',
layer: 'terrain'
}
},
{
name: 'Bing Maps Road',
active: false,
source: {
name: 'Bing Maps',
type: 'BingMaps',
key: 'Aj6XtE1Q1rIvehmjn2Rh1LR2qvMGZ-8vPS9Hn3jCeUiToM77JFnf-kFRzyMELDol',
imagerySet: 'Road'
}
},
{
name: 'Mapbox Geography Class',
active: false,
source: {
type: 'TileJSON',
url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
}
}
],
changeLayer: function(layer) {
$scope.layers.map(function(l) {
l.active = (l === layer);
});
}
});
}
})();
My html file is like below:
<nav id="couches">
<md-radio-group>
<md-radio-button ng-repeat="layer in layers" ng-click="changeLayer(layer)">{{ layer.name }}<br/></md-radio-button>
</md-radio-group>
</nav>
When i execute, i get just list of the names of the layers and when i click on a layer's name i see the layer correctly. But no radio buttons ! What should i fix to solve this problem?

Angular-Leaflet-Directive: Set opacity of a layer

I am using the angular-leaflet-directive. Is there an option to change the opacity of a layer?
With 'regular' leaflet I can use layer.setOpacity(0.5). But there seems to be no option in the angular-leaflet-directive.
Edit:
here is my leaflet configuration:
angular.module('epic-taxi')
.controller('MainController', ['$scope', function($scope) {
$scope.initMap = function() {
angular.extend($scope, {
newYork: {
lat: 40.7304783951045,
lng: -73.98880004882812,
zoom: 12
},
layers: {
baselayers: {
mapbox_light: {
name: 'Light',
type: 'xyz',
url: 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'
}
},
overlays: {
subway: {
name: 'Subway',
visible: true,
type: 'group',
opacity: 0.1 // not working
}
}
}
});
};
}]);
I want to change the opacity of the 'subway' overlay which contains multiple paths.
It looks like this is not implemented yet:
https://github.com/tombatossals/angular-leaflet-directive/issues/251
Thanks to Jonatas Walker for pointing it out!
Did you read their docs?
app.controller("CenterController", [ '$scope', function($scope) {
angular.extend($scope, {
center: {
lat: 40.095,
lng: -3.823,
zoom: 4,
opacity: 0.5
},
defaults: {
scrollWheelZoom: false
}
});
}]);
Try using this:
overlays: {
subway: {
name: 'Subway',
visible: true,
type: 'group',
layeroptions: {
opacity: 0.1
}
},
}

How to share code in angular js (concept of code resuability in angular js?

i am new to angular js and i want to share and make certain codes reusable. i have tried to do that using services and factory. But i am getting error.
'use strict';
angular.module('myApp.ctrls')
.controller('Ctrl_HubStockOnHandMode', function($http, $scope, reportService) {
$scope.HubStockOnHandModeGridOptions = {
dataSource: {
type: "jsonp",
transport: {
read: function(e) {
reportService.WebAPI('abc/BIUMo', {
EnvironmentCode:'JMVH',
OrderBy: getOrderBy(e.data.sort)
}).then(function (d) {
$scope.data = d;
e.success(d.Data);
});
}
},
serverPaging: true,
serverSorting: true
},
height:config.GridHeight,
scrollable:true,
sortable: {
mode: "single",
allowUnsort: true
},
filterable: {
extra: false,
operators: {
string: {
startswith: "Starts with",
eq: "Is equal to",
neq: "Is not equal to",
contains: "Contains"
}
}
},
pageable: false,
columns: [
{
field: "RowNumber"
,title: "No."
,width: "50px"
,sortable:false
,filterable: false
},
{
field: 'ItemCTSH'
,title:'Item'
,template: "<div kendo-tooltip title='#= ItemCT #' > #= ItemCTSH # </div>"
,filterable: {
ui: itemFilter
}
},
]
};
}
//Get Item List
$http.get('http://webapi.dashboard.hcmisonline.org/api/OID_WebApi/IssuedItemList')
.success(function (data) {
$scope.items = data.Data;
});
function itemFilter(element) {
element.kendoAutoComplete({
dataSource: $scope.items
});
}
}
});
I want to reuse the functions like the Get item. i have other pages/grids that use exactly this code, except changing the environment code
how to i solve this issue?
thanks

Resources