【问题标题】:How to remove undefined error in angular js?如何删除角度 js 中的未定义错误?
【发布时间】:2015-12-24 07:12:54
【问题描述】:

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

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)

链接器 http://plnkr.co/edit/siqCUS3jUKVQz6pxmC50?p=preview

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 正是 ;) 这就是解决的本质——返回一些东西 ;)
    【解决方案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:如何在解析完成之前显示一些视图...正在加载...?

    基于我扩展的一些 cmets the example here。它现在显示这个视图:

    加载B.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'); 
    }
    

    并且还让 'laodingB' 的 'b' 孩子

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

    全部查看here

    【讨论】:

    • 我需要显示加载器,直到所有服务数据都不会来
    • 嗯,UI-Router resolve: {} 设置是为了工作......方式,该状态不会发出,直到所有数据都加载(解决)。
    • 我们可以显示这个ionicframework.com/docs/api/service/$ionicLoading中给出的加载器
    • 让我检查一下,以确保我理解你
    • 我想我需要使用 $q.all()。如果所有数据都来了,我需要隐藏 loader ..或者我可以使用其他方法
    【解决方案3】:

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

    .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 演示 here

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多