javascript - switch views from one page to another when the button is clicked in angular JS -


i new angular js.how can redirect page when button clicked. code here

var app = angular.module("plunker", []) .config(function ($routeprovider, $locationprovider, $httpprovider) {  $routeprovider.when('/home', {   templateurl:    'home.html',   controller:     'homectrl' }); $routeprovider.when('/about', {   templateurl:    'about.html',   controller:     'aboutctrl' }); $routeprovider.when('/contact', {   templateurl:    'contact.html',   controller:     'contactctrl' }); $routeprovider.otherwise( {   redirectto:     '/home',   controller:     'homectrl',  } 

); });

app.controller('navctrl',  ['$scope', '$location', function ($scope, $location) {   $scope.navclass = function (page) { var currentroute = $location.path().substring(1) || 'home'; return page === currentroute ? 'active' : ''; };    }]);  app.controller('aboutctrl', function($scope, $compile) { console.log('inside controller');   });  app.controller('homectrl', function($scope, $compile) { console.log('inside home controller'); 

//redirect when button click function cntrl ($scope,$location) { $scope.redirect = function(){ window.location.href = '/about'; } }

});

app.controller('contactctrl', function($scope, $compile) { console.log('inside contact controller');

});

my html markup is

<div ng-controller="cntrl">     <button class="btn btn-success" ng-click="changeview('about')">click me</button> </div> 

you entered :

how this.help me solve .

just use standard html link :

<div ng-controller="cntrl">     <a class="btn btn-success" ng-href="#/about">click me</a>         </div> 

no need create scope function that. can handle dynamically ng-href :

<div ng-controller="cntrl">     <a class="btn btn-success" ng-href="#/{{view}}">click me</a>         </div> 

last thing, should consider using ui-router handle better cases


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -