Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am creating a basic slider using HTTP service from a local JSON file. Everything works if I don't use ui.router, when implementing ui.router, routing is working but the slider(next, previous button) is not changing the data .I have my route.js and controller.js below is my code. i consoled object and is logging. but not throwing any error scope.showprice,showavail etc and see the data changing in the log. but not in the ui view.

app.controller('myctrl',function ($scope,$http) {

    $scope.productpage =" Product Page";
    $scope.gcpro =" Gc-pro Page";
    $scope.lessons =" Lessons Page";
    $scope.outlet =" Outlet Page";
    $scope.platinum ="The platinum Page";
    $scope.rental =" Rental Page";
    $scope.repairs =" Repairs Page";
    $scope.usedgear ="Usedgear Page";
    $scope.vintage =" Vintage Page";

    var jdata=0;
    var ctrl =this;
    $scope.reveal= function (){

        $scope.showsub=true;


    }

    $scope.notreveal= function(){

        $scope.showsub=false;
    }

    $http.get("../json/myJson.json")
        .then(function(response) {
            ctrl.sdata = response.data;
            console.log(ctrl.sdata);

            $scope.showprice = ctrl.sdata.allProducts[0].price;
            $scope.showavail = ctrl.sdata.allProducts[0].stockavailability;
            $scope.showdescribe= ctrl.sdata.allProducts[0].discription;
            $scope.showreview = ctrl.sdata.allProducts[0].custmerreviews;
            $scope.showimage = ctrl.sdata.allProducts[0].image;






        });

   $scope.slider= function (i) {


        $http.get("../json/myJson.json")
            .then(function(response) {
                ctrl.sdata = response.data;

                jdata = jdata+i;

                if (jdata < 0) {

                    jdata = ctrl.sdata.allProducts.length - 1;
                }

                if (jdata > ctrl.sdata.allProducts.length - 1) {

                    jdata = 0;
                }

                $scope.showprice = ctrl.sdata.allProducts[jdata].price;
                $scope.showavail = ctrl.sdata.allProducts[jdata].stockavailability;
                $scope.showdescribe = ctrl.sdata.allProducts[jdata].discription;
                $scope.showreview = ctrl.sdata.allProducts[jdata].custmerreviews;
                $scope.showimage = ctrl.sdata.allProducts[jdata].image;
console.log($scope.showprice);
            })
    }

});

app.config(function ($stateProvider,$urlRouterProvider,    $locationProvider
) {



    $urlRouterProvider.otherwise('/home')


    $stateProvider.state('home',
        {

            url: '/home',
            templateUrl: '../html/home.html',
            controller: 'myctrl'


        })

        .state('products',

            {

                url: '/products',
                templateUrl: '../html/products.html',
                controller: 'myctrl'


            })
        .state('gcpro',

            {

                url: '/gcpro',
                templateUrl: '../html/gcpro.html',
                controller: 'myctrl'


            })
        .state('lessons',

            {

                url: '/lessons',
                templateUrl: '../html/lessons.html',
                controller: 'myctrl'


            })
        .state('outlet',

            {

                url: '/outlet',
                templateUrl: '../html/outlet.html',
                controller: 'myctrl'


            })
        .state('platinum',

            {

                url: '/platinum',
                templateUrl: '../html/platinum.html',
                controller: 'myctrl'


            })
        .state('rental',

            {

                url: '/rental',
                templateUrl: '../html/rental.html',
                controller: 'myctrl'


            })
        .state('repairs',

            {

                url: '/repairs',
                templateUrl: '../html/repairs.html',
                controller: 'myctrl'


            })
        .state('usedgear',

            {

                url: '/usedgear',
                templateUrl: '../html/usedgear.html',
                controller: 'myctrl'


            })
        .state('vintage',

            {

                url: '/vintage',
                templateUrl: '../html/vintage.html',
                controller: 'myctrl'


            })

})

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title></title>


    <link rel="stylesheet" type="text/css" href="../css/css.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script src="//unpkg.com/angular-ui-router/release/angular-ui-router.min.js"></script>
</head>

<body  >
<div id="wraper"  >
    <div id="header">

        <img src="../images/guitar1.png" id="topscreenshot" >
        <input type="text" id="topinput" placeholder="Enter keywords, Item # or Catalog #">

    </div>

    <div id="mainpage" ng-controller="myctrl as ctrl">

    <div id="navbardiv">

<span id="allnavbars" ng-mouseenter="reveal()" ng-mouseleave="notreveal()">

    <a ui-sref ="home"><button class="navbarbuttons">Home</button></a>
     <a ui-sref ="products"><button class="navbarbuttons">Product</button></a>
        <a ui-sref ="usedgear"><button class="navbarbuttons">Used Gear</button></a>
          <a ui-sref ="platinum"><button class="navbarbuttons">Platinum</button></a>
             <a ui-sref ="vintage"><button class="navbarbuttons">Vintage</button></a>
                <a ui-sref ="outlet"> <button class="navbarbuttons">Outlet</button></a>
                    <a ui-sref ="gcpro"> <button class="navbarbuttons">GC Pro</button></a>
                        <a ui-sref ="lessons"><button class="navbarbuttons">Lessons</button></a>
                            <a ui-sref ="repairs"><button class="navbarbuttons">Repairs</button></a>
                                <a ui-sref ="rental"><button class="navbarbuttons">Rentals</button></a>

      </span>

    </div>

    <div id="undernavbargrey">
      <span id="dropdownspan" ng-show="showsub" >

       <a href="#" class="anchorsss">Guitars</a>
       <a href="#" class="anchorsss">Bass</a>
       <a href="#" class="anchorsss">Amps Effects</a>
       <a href="#" class="anchorsss">Drums</a>
       <a href="#" class="anchorsss">Recording</a>
       <a href="#" class="anchorsss">Software</a>
       <a href="#" class="anchorsss">keyboards MIDI</a>
       <a href="#" class="anchorsss">Mics Wireless</a>
       <a href="#" class="anchorsss">Live Sound</a>
       <a href="#" class="anchorsss">DJ</a>
       <a href="#" class="anchorsss">Lighting</a>
       <a href="#" class="anchorsss">Accessories</a>
       <a href="#" class="anchorsss">Band Orchestra</a>
       </span>

    </div>

    <div id="greyline"></div>

    <div id="leftpadamdiv"> <img src="../images/left.png" id="leftpadam" ng-click="slider(-1)" ></div>

    <div id="rightpadamdiv"><img src="../images/right.png" id="rightpadam" ng-click="slider(1)" ></div>

    </div>


<div ui-view>



</div>



    <div id="therestdiv">
        <img src="../images/therest.png" id="therestpic">
    </div>







</div>


<script src = "../script/module.js"></script>
<script src="../script/controller.js"></script>
<script src="../script/route.js"></script>


<script src="../script/cdirective.js"></script>




</body>
</html>

enter image description here

share|improve this question
    
what's the version of ui-router you are using? – Sravan Jan 16 at 5:08
    
hey i accidentally had the old version of ui router. so the transition problem was fixed – Anu Rajan Jan 16 at 6:49
    
But the data still not changing. i updated the question please read if you can – Anu Rajan Jan 16 at 6:55
    
is $http.get("../json/myJson.json") this file is getting called? check network tab once. – Sravan Jan 16 at 7:05
    
yes that's the file . I console log all the response and the data is changing in console. So the api call is working . When i removed ui. router, ui view and use simple html binding, then it work. – Anu Rajan Jan 16 at 8:20
up vote 1 down vote accepted

First issue you posted is the issue with your ui-router version. Please update the ui-router version as your current version doesn't handle transition issue.

Here is the reference for that issue

For the second issue, check if that Json file is getting called,

use $resource to fetch the data from a json file.

$scope.slider= function (i) {
    $resource('app/data.json', {}, {
        query: {
        method: 'GET',
        }
    })
    .then(function(response) {
        ctrl.sdata = response.data;

        jdata = jdata+i;

        if (jdata < 0) {

            jdata = ctrl.sdata.allProducts.length - 1;
        }

        if (jdata > ctrl.sdata.allProducts.length - 1) {

            jdata = 0;
        }

        $scope.showprice = ctrl.sdata.allProducts[jdata].price;
        $scope.showavail = ctrl.sdata.allProducts[jdata].stockavailability;
        $scope.showdescribe = ctrl.sdata.allProducts[jdata].discription;
        $scope.showreview = ctrl.sdata.allProducts[jdata].custmerreviews;
        $scope.showimage = ctrl.sdata.allProducts[jdata].image;
        console.log($scope.showprice);
    })
}

Dont forget to addngResourcein the module.

Edit: I prefer to add the api calls and the data json calls in a separate service.

Here is an example of such service:

(function(myApp) {
    'use strict';

        myApp.factory('DataService',DataService);

        function DataService($resource)
            {
            return $resource('app/data.json', {}, {
                query: {
                method: 'GET',
            }
        });
    }
})(myApp);

Home.html:

    <div id="greyline"></div>

        <div id="leftpadamdiv"> <img src="../images/left.png" id="leftpadam" ng-click="slider(-1)" ></div>

        <div id="rightpadamdiv"><img src="../images/right.png" id="rightpadam" ng-click="slider(1)" ></div>

        </div>
.........
........
// your remaining code

Index.html

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/css.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script src="//unpkg.com/angular-ui-router/release/angular-ui-router.min.js"></script>
</head>
<body>
<div id="wraper"  >
    <div id="header">
        <img src="../images/guitar1.png" id="topscreenshot" >
        <input type="text" id="topinput" placeholder="Enter keywords, Item # or Catalog #">
    </div>
    <div id="mainpage">
    <div id="navbardiv">
<span id="allnavbars" ng-mouseenter="reveal()" ng-mouseleave="notreveal()">
    <a ui-sref ="home"><button class="navbarbuttons">Home</button></a>
     <a ui-sref ="products"><button class="navbarbuttons">Product</button></a>
        <a ui-sref ="usedgear"><button class="navbarbuttons">Used Gear</button></a>
          <a ui-sref ="platinum"><button class="navbarbuttons">Platinum</button></a>
             <a ui-sref ="vintage"><button class="navbarbuttons">Vintage</button></a>
                <a ui-sref ="outlet"> <button class="navbarbuttons">Outlet</button></a>
                    <a ui-sref ="gcpro"> <button class="navbarbuttons">GC Pro</button></a>
                        <a ui-sref ="lessons"><button class="navbarbuttons">Lessons</button></a>
                            <a ui-sref ="repairs"><button class="navbarbuttons">Repairs</button></a>
                                <a ui-sref ="rental"><button class="navbarbuttons">Rentals</button></a>

      </span>

    </div>

    <div id="undernavbargrey">
      <span id="dropdownspan" ng-show="showsub" >

       <a href="#" class="anchorsss">Guitars</a>
       <a href="#" class="anchorsss">Bass</a>
       <a href="#" class="anchorsss">Amps Effects</a>
       <a href="#" class="anchorsss">Drums</a>
       <a href="#" class="anchorsss">Recording</a>
       <a href="#" class="anchorsss">Software</a>
       <a href="#" class="anchorsss">keyboards MIDI</a>
       <a href="#" class="anchorsss">Mics Wireless</a>
       <a href="#" class="anchorsss">Live Sound</a>
       <a href="#" class="anchorsss">DJ</a>
       <a href="#" class="anchorsss">Lighting</a>
       <a href="#" class="anchorsss">Accessories</a>
       <a href="#" class="anchorsss">Band Orchestra</a>
       </span>
    </div>

<div ui-view>
</div>

<div id="therestdiv">
    <img src="../images/therest.png" id="therestpic">
</div>
</div>
<script src = "../script/module.js"></script>
<script src="../script/controller.js"></script>
<script src="../script/route.js"></script>
<script src="../script/cdirective.js"></script>
</body>
</html>

Check I removed controller in index.html

share|improve this answer
    
can you paste the view? – Sravan Jan 16 at 10:00
    
i added a screen shot of my log also put html . please check – Anu Rajan Jan 16 at 15:03
    
what are the lines you console' d? and where did you use ctrl.sdata, I mean in which controller? – Sravan Jan 17 at 4:39
    
i used ctrl.sdata in ui view. so controller is defined in the state and using a home.html to show. the array 0 is showing like the picture. (i have update the question with more picture ,i am not sure if you saw)The price,guitar picture, description etc are showing . its just not showing array 1,2,3,4.. when clicking next button ,eventhough when console logging , the array is changing( see the log in the picture) – Anu Rajan Jan 17 at 6:56
    
so when i did "console.log($scope.showprice)" each price is chaning in the log from array 0-7 when clicking next button, but not updating in the view. The view expressed in home.html is working . i tested it by pasting directly without using ui router. but when using ui router the view is not updating even thou its loging – Anu Rajan Jan 17 at 6:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.