简体   繁体   English

TypeError:无法读取未定义的属性'addTopic'。 我是盲人吗?

[英]TypeError: Cannot read property 'addTopic' of undefined. Am I blind?

since I've been staring at this problem for some days now, I'm kinda new at AngularJS, I thought maybe someone here could help me. 因为我已经盯着这个问题几天了,所以我对AngularJS很陌生,我想也许这里有人可以帮助我。 So to my problem: 所以对我的问题:

I get a Typeerror when i try to save a new topic on a forum I'm creating: My controller 当我尝试在正在创建的论坛上保存新主题时出现Typeerror:我的控制器

module.controller('newTopicController', ['$scope', '$http', 'dataService', function ($scope, $http, $window, dataService) {
$scope.newTopic = {};

$scope.save = function () {

    dataService.addTopic($scope.newTopic)
    .then(function () {
        $window.location = "/#";
    },
    function () {
        alert("couldnt save topic");
    });
};
}]);

And my factory: 而我的工厂:

module.factory("dataService", function ($http, $q) {

var _topics = [];
var _isInit = false;

var _isReady = function () {
    return _isInit;
};

var _getTopics = function () {

    var deferred = $q.defer();

    $http.get("/api/topics?withReplies=true")
    .then(function (result) {
        angular.copy(result.data, _topics);
        _isInit = true;
        deferred.resolve();
    },
    function () {
        deferred.reject();
    });

    return deferred.promise;
};

var _addTopic = function (newTopic) {
    var deferred = $q.defer();

    $http.post("/api/topics", newTopic)
   .then(function (result) {
       var createdTopic = result.data;
       _topics.splice(0, 0, createdTopic);
       deferred.resolve(createdTopic);
   },
   function () {
       deferred.reject();
   });

    return deferred.promise;
};

return {
    topics: _topics,
    getTopics: _getTopics,
    addTopic: _addTopic,
    isReady: _isReady
};
});

So when i try to add a topic to the forum I just get "TypeError: Cannot read property 'addTopic' of undefined" in the controller, right where dataService.addTopic($scope.newTopic) is. 因此,当我尝试将主题添加到论坛时,我只是在控制器中的dataService.addTopic($ scope.newTopic)所在的位置收到“ TypeError:无法读取未定义的属性'addTopic'of undefined”。

I also have another controller who also uses the factory, but that shouldnt be a problem right? 我还有另一个也使用工厂的控制器,但这不应该是一个问题吧?

Thanks for your time. 谢谢你的时间。

This seems incorrect: 这似乎不正确:

module.controller('newTopicController', ['$scope', '$http', 'dataService', function ($scope, $http, $window, dataService) {...}

Change it to: 更改为:

module.controller('newTopicController', ['$scope', '$http', '$window', 'dataService', function ($scope, $http, $window, dataService) {...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义的属性“then”。 当我尝试运行 updatefirst 函数时出现此错误 - TypeError: Cannot read property 'then' of undefined. Getting this error when I am trying to run updatefirst function “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 无法读取未定义的属性“插入”。 我发布不正确吗? - Cannot read property 'insert' of undefined. Am I not publishing correctly? 无法读取未定义的属性“toFixed”。 未捕获的类型错误 - Cannot read property 'toFixed' of undefined. Uncaught TypeError Node / AngularJS-TypeError:无法读取未定义的属性“ _id”。 - Node/AngularJS - TypeError: Cannot read property '_id' of undefined. 类型错误:无法读取未定义的属性“地图”。 反应 js - TypeError: Cannot read property 'map' of undefined. React js 类型错误:无法读取未定义的属性“状态”。 更新反应 - TypeError: Cannot read property 'state' of undefined. Updated React TypeError:无法读取未定义的属性“标题”。 Vue.js - TypeError: Cannot read property 'title' of undefined. Vue.js TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue? TypeError:无法读取未定义的属性“ 0”。 React Redux应用 - TypeError: Cannot read property '0' of undefined. React Redux App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM