Angular JS Passing Parameters to URL in UI-Router
Submitted by nurhodelta_17 on Monday, January 15, 2018 - 18:52.
Getting Started
I've used CDN for Bootstrap, Angular JS and UI-Router so you need internet connection for them to work.Creating our Database
First we are going to create our database and insert data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.index.html
This is the main view of our app.app.js
This is our main angular js script.- var app = angular.module('app', ['ui.router']);
- app.config(function($stateProvider, $urlRouterProvider) {
- $urlRouterProvider.otherwise('/home');
- $stateProvider
- .state('home', {
- url: '/home',
- templateUrl: 'home.html',
- controller: 'homeCtrl'
- })
- //dis is our state where we put our id in url
- .state('view', {
- url: '/view/{member:json}',
- params: {member: null},
- templateUrl: 'view.html',
- controller: 'viewCtrl'
- })
- });
- app.factory('memberService', function($http){
- return{
- fetchAllMember: function(){
- var members = $http.get('members.php');
- return members;
- }
- }
- });
- app.controller('homeCtrl', ['$scope', '$state', 'memberService', function($scope, $state, memberService) {
- var allmembers = memberService.fetchAllMember();
- allmembers.then(function(response){
- $scope.members = response.data;
- });
- $scope.viewMember = function(member){
- $state.go('view', {member:member});
- }
- }]);
- app.controller('viewCtrl', function($scope, $stateParams) {
- $scope.details = $stateParams.member;
- });
home.html
Per our router, this can be treated as our index view.view.html
This is the view where we show the object that we sent as parameters in url.members.php
This is our PHP api that fetches data from our MySQL database.- <?php
- $conn = new mysqli("localhost", "root", "", "angular");
- $sql = "SELECT * FROM members";
- $query=$conn->query($sql);
- while($row=$query->fetch_array()){
- $output[] = $row;
- }
- ?>
Add new comment
- 47 views