Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to remove undefined error in angular js ?Actually i am trying to load data in using resolve and use that data in controller but on controller i am getting undefined why ?

resolve: {
    message: function (testservice) {
        return testservice.getdata();
    },
    message2: function (testservice) {
        return testservice.getTodo();
    },
    message3: function (testservice) {
        return testservice.getGithub();
    }
}

use the controller

.controller('b', function($scope, $stateParams, $state, $ionicHistory,message) {
    console.log("controller is loaded"+message)
})

// undefined in this console

console.log("controller is loaded"+message)

plinker http://plnkr.co/edit/siqCUS3jUKVQz6pxmC50?p=preview

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would say, that you are almost there.

Just the service/factory must return something. I mean here:

...
message: function(testservice) {
      return testservice.getdata();
}

we expect something... and there was nothing

I added one line return data; and there is that updated plunker

.factory('testservice', ['$http',
  function testservice($http) {
    // interface

    // implementation
    function getData() {

      return $http.get("http://jsonplaceholder.typicode.com/photos")
        .then(function(data) {
          console.log(data)

          // HERE - this line was missing
          // return something here
          return data;

        }, function(error) {
          console.log(error)
        })

EXTEND: How to show some view ...loading... before resolve is done?

Based on some comments I extended the example here. It is now showing this view:

loadingB.html

<div >
  <div ui-view="">
    <h2>...loading...</h2>
  </div>
</div>

which is a view of a brand new parent state 'loadingB':

.state('loadingB', {
  redirectTo: "b",
  templateUrl : "loadingB.html",
})

It injects the above view loadingB.html in position of original state 'b'. And it has one property redirectTo: "b", which is managed by this small piece of code:

.run(['$rootScope', '$state', function($rootScope, $state) {
    $rootScope.$on('$stateChangeSuccess', function(evt, to, params) {
      if (to.redirectTo) {
        evt.preventDefault();
        $state.go(to.redirectTo, params)
      }
    });
}])

And our service now uses $timeout to get some delay:

.factory('testservice', ['$http', '$timeout',
  function testservice($http, $timeout) { 
    // interface

    // implementation
    function getData() { 

      return $http.get("http://jsonplaceholder.typicode.com/photos")
        .then(function(data) {
          console.log("resolved http")
          return $timeout(function(){
            console.log("after two seconds delay")
            return data;
          }, 2500)
        ...

And finally, we have to redirect to loadingB here

$scope.moveto = function() {
    $state.go('loadingB'); 
}

And also make the 'b' child of 'laodingB'

.state("b", {
  parent: "loadingB", 
  templateUrl: "b.html",
  url: "/b",
  controller: 'b', 
  resolve: { 
    message: function(testservice) {
      return testservice.getdata();
    },

Check it all here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...