【问题标题】:Unresolved function or method then() AngularJS未解析的函数或方法 then() AngularJS
【发布时间】:2017-10-13 10:22:07
【问题描述】:

我正在使用以下代码并收到错误:Unresolved function or method then()。 我不知道这里出了什么问题。我正在使用 WebStorm 11。我在我的脚本。

这是我的控制器及以下:

RecordApp.controller('MyController',
['$scope','recordaccess',function($http,$route,$scope,recordaccess,                   
$location,$rootScope,$cookieStore) {
recordaccess.then(function(data) {------>Here I am getting error.
.
.
.
});

这是我用来从 JSON 中获取数据的:

RecordApp.factory('recordaccess', ['$http', function($http) {
return $http.get('record1.json')
.then(function successCallback(response) {------>Here I am getting error.
return response.data;
},
function errorCallback(response) {
alert("Error occurred. Status: " + 
response.status + " - " + response.statusText);
});
}]);

【问题讨论】:

  • 注入列表与函数参数不匹配。试试 ['$http', '$route', '$scope','recordaccess','$location','$rootscope','$cookieStore',function($http,$route,$scope,recordaccess , $location,$rootScope,$cookieStore)
  • @atao 我试过用这个但没有运气。

标签: angularjs json


【解决方案1】:

你的工厂声明是错误的,例如:

module.factory('MyFactory', function() {

    var factory = {}; 

    factory.method1 = function() {
            //..
        }

    factory.method2 = function() {
            //..
        }

    return factory;
});

然后你可以像这样使用它:

MyFactory.method1().then(function(data){...});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2022-12-03
    • 2014-03-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多