javascript - Angular-JS routing get data from MVC controller -
i have implemented code retrieve data mvc controller using angular-js
using ngroute
. have implemented is, have 2 action methods in mvc controller , @ client side have 2 menu buttons , clicking on each button, retrieves data respective action method. have used angular-route
, angular factory method
, ng-view
till fine. can see angularjs
doesn't go action method every time when click on button. once data have been retrieved, shows respective view. , in situation cannot retrieve fresh data database. if have call action method every time how can achieve ?
this have implemented:
accountcontroller:
public actionresult getaccounts() { var repo = new accountrepository(); var accounts = repo.getaccounts(); var settings = new jsonserializersettings { contractresolver = new camelcasepropertynamescontractresolver() }; var jsonresult = new contentresult { content = jsonconvert.serializeobject(accounts, settings), contenttype = "application/json" }; return jsonresult; } public actionresult getnumbers() { var repo = new accountrepository(); var numbers = repo.getnumbers(); var setting = new jsonserializersettings { contractresolver = new camelcasepropertynamescontractresolver() }; var jsonresult = new contentresult() { content = jsonconvert.serializeobject(numbers, setting), contenttype = "application/json" }; return jsonresult; }
account.js:
'use strict'; var account = angular.module('accountmodule', ['ngroute']); account.config(['$routeprovider', function ($routeprovider) { $routeprovider.when('/employees', { templateurl: '../templates/employees.html', controller: 'accountcontroller' }) .when('/numbers', { templateurl: '../templates/numbers.html', controller: 'numbercontroller' }) .otherwise({ redirectto: '/phones' });; }]); account.controller('accountcontroller', [ '$scope', 'accountservice', function ($scope, accountservice) { accountservice.getemployees().then(function(talks) { $scope.employees = talks; }, function() { alert('some error'); }); } ]); account.controller('numbercontroller', [ '$scope', 'accountservice', function ($scope, accountservice) { accountservice.getnumbers().then(function (retrievednumbers) { $scope.telephonenumbers = retrievednumbers; }, function () { alert('error while retrieving numbers'); }); } ]);
accountservice.js:
account.factory('accountservice', ['$http', '$q', function ($http, $q) { var factory = {}; factory.getemployees = function () { var deferred = $q.defer(); $http({ method: 'get', url: 'getaccounts' }).success(deferred.resolve).error(deferred.reject); return deferred.promise; }; factory.getnumbers = function () { var deferred = $q.defer(); $http({ method: 'get', url: 'getnumbers' }).success(deferred.resolve).error(deferred.reject); return deferred.promise; }; return factory; }]);
and view this:
@{ viewbag.title = "index"; layout = "~/views/shared/_layout.cshtml"; } <script type="text/javascript" src="../../scripts/modules/account/account.js"></script> <script type="text/javascript" src="../../scripts/modules/account/accountservice.js"></script> <div ng-app="accountmodule"> <div> <div class="container"> <div class="navbar navbar-default"> <div class="navbar-header"> <ul class="nav navbar-nav"> <li class="navbar-brand"><a href="#/employees">employees</a></li> <li class="navbar-brand"><a href="#/numbers">numbers</a></li> </ul> </div> </div> <div ng-view> </div> </div> </div> </div>
Comments
Post a Comment