如何删除未定义的错误角JS? [英] How to remove undefined error in angular js?

查看:127
本文介绍了如何删除未定义的错误角JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何角JS除去不确定的错误?其实我试图用决心来加载数据,并使用这些数据的控制器,但控制器我得到的未定义?为什么?

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();
    }
}

使用控制器

.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=$p$pview

推荐答案

我会说,你是几乎没有。

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

我增加了一个行 返回数据; 并存在的已更新的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)
        })

扩展?如何表现出一定的看法...加载中...决心完成之前

基于一些意见我延长 这里的例子 <

/ A>。现在它显示出这样的观点:

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

loadingB.html

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

这是一个全新的父状态的 'loadingB'的看法

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

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

它注入在原始状态B的位置,上述观点loadingB.html。它有一个属性的 redirectTo:

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

最后,我们要重定向到 loadingB 此处

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

,也使laodingB

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();
    },

检查所有这里

这篇关于如何删除未定义的错误角JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆