Arun
Arun

Reputation: 11

Routing in AngularJS is not working for me

<script>
var app = angular.module('myApp', ['ngRoute']);

app.config(function($routeProvider){
    $routeProvider.when('/dataBinding', {
        template: `
          <div ng-controller="myCtrl">
            <h3>Data Binding controller</h3>
            <input type="text" ng-model="firstName">
            <input type="text" ng-model="lastName">
            <br><br>
            {{ firstName + ' ' + lastName }}
          </div>
        `,
        controller: 'myCtrl'
      }).when('/httpapi', {
        template: `
          <div ng-controller="apiCtrl">
            <h3>$https API Controller</h3>
            <p>A fact From API: {{ reqCode }}</p>
          </div>
        `,
        controller: 'apiCtrl'
      })
      .when('/cusService', {
        template: `
          <div ng-controller="customeServiceCtrl">
            <h3>Custom Service API call Controller</h3>
            <p>Genderize API using Custom Service: {{ cusserviceData }}</p>
          </div>
        `,
        controller: 'customeServiceCtrl'
      });
})

app.controller('apiCtrl',function($scope,$http){
    $http({
      method:'get',
      url:'https://catfact.ninja/fact'  
    }).then(function(response){
      
      $scope.reqCode = response.data.fact;
    })
})


app.controller('customeServiceCtrl',function($scope,myService){
        $scope.myData = {"email": "[email protected]","password": "cityslicka"};
    myService.resData($scope.myData).then(function(token) {
      $scope.cusserviceData = token;
    })
})
</script>
<div>
  <a href="#/dataBinding">Data Binding</a>
  <a href="#/list">List</a>
  <a href="#/httpapi">$https API</a>
  <a href="#/cusService">Custom Service API call</a>
</div>
<div ng-view></div>

routing is not working in my file. i have used all the cdn aswell. other than routing config all are working fine, like services, controllers and custom directives, etc,. Please someone find the issue and rectify me . iam new to angular js.

Upvotes: 1

Views: 23

Answers (0)

Related Questions