如何删除角度js中的未定义错误?

时间:2015-09-26 16:31:13

标签: javascript angularjs angularjs-directive angularjs-scope angular-ui-router

如何删除角度js中的未定义错误?实际上我正在尝试使用resolve加载数据并在控制器中使用该数据但在控制器上我得到未定义为什么?

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

使用控制器

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

//在此控制台中未定义

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

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

3 个答案:

答案 0 :(得分:2)

我会说,你几乎就在那里。

只是服务/工厂必须返回一些东西。我的意思是:

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

我们期待什么......而且没有什么

我添加了一行 return data; 并且有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:如何在解析完成之前显示一些视图... loading ...

基于一些评论,我延长了the example here。它现在显示了这个观点:

loadingB.html

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

这是一个全新的母州 'loadingB' 的视图:

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

它将上面的视图loadingB.html注入原始状态'b'的位置。它有一个属性 redirectTo: "b" ,由一小段代码管理:

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

我们的服务现在使用 $timeout 来延迟:

.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)
        ...

最后,我们必须重定向到loadingB这里

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

并且还将'b'孩子变为'laodingB'

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

全部检查here

答案 1 :(得分:1)

Radim Koehler有正确的答案,或者更简单,只需从服务中的getData函数返回promise:

function getData() { 
      return $http.get("http://jsonplaceholder.typicode.com/photos");
}

答案 2 :(得分:0)

如果你想在进入视图之前解决状态,那么你需要做一些事情。解决你这样的状态

.state('app.b', {
  url: "/b",
  views: {
    'menuContent': {
      templateUrl: "b.html",
      controller: 'b',
      resolve: {
        apiData: "testservice",
        itemDetailsData: function($q, apiData) {
          var item = apiData.getdata();
          if (item) {
            return $q.when(item);
          } else {
            return $q.reject();
          }
        }
      }
    }
  }
})

在您的服务中,您可以使用工厂方法,如:

.factory('albumService', ['$http', '$q', '$ionicLoading', '$timeout', albumFn])
function albumFn($http, $q, $ionicLoading, $timeout) {
return {
getAlbums: function() {
  $ionicLoading.show({
    content: '<i class="icon ion-loading-d"></i>',
    animation: 'fade-in',
    showBackdrop: true,
    maxWidth: 200,
    showDelay: 5
  });

  var def = $q.defer();
  $http.get("data.json")
    .success(function(data) {
     $ionicLoading.hide();
      def.resolve(data);
    })
    .error(function() {
     $ionicLoading.hide();
      def.reject("Failed to get albums");
    });

  return def.promise;

    }
  }
 }

我创建了类似的plunker demo here