"类型错误:未定义不是一个函数"调用从工厂函数时 [英] "TypeError: undefined is not a function" when calling a function from the factory

查看:251
本文介绍了"类型错误:未定义不是一个函数"调用从工厂函数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给在通过控制器工厂定义的函数,而是得到一个错误:


  

类型错误:未定义不是一个函数


可能有人帮助,请帮助我吗?

  //服务
angular.module('博客')工厂('POSTDATA',函数($ HTTP){
  变种POSTDATA = {}
  postData.data.posts = [{
    标题:第一个冠军,
    内容:第一个内容
  }]
  postData.loadPosts =功能(){
    postData.data.posts = $ http.get('./ posts.json')
      .success(功能(数据){
        postData.data.posts =数据;
      })
      .error(函数(){
        console.error('无法加载数据');
      })
  }
  postData.createPost =功能(newPost){
    VAR的数据;
    如果(newPost.newPostTitle ===''|| newPost.newPostContents ===''){
      警报('无论是名称也不身体允许被留为空白。');
      返回false;
    }
    数据= {
      最新帖子: {
        标题:newPost.newPostTitle,
        内容:newPost.newPostContents
      }
    };
    $ http.post('./ posts.json',数据).success(功能(数据){
      postData.data.posts.push(数据);
      的console.log('成功创建后。');
    })错误(函数(){
      console.error(无法创建新的岗位。);
    });
    返回true;
  };  的console.log(初始化POSTDATA);
  返回POSTDATA;
})
//控制器
VAR createPostCtrl =功能($范围,$位置,POSTDATA){
  POSTDATA = POSTDATA;
  $ scope.data = postData.data;
  postData.loadPosts();
  $ scope.formData = {
    newPostTitle:'',
    newPostContents:''
  };
  $ scope.createPost =功能(){
    //这一个是抛出一个错误类型错误:未定义不是一个函数
    postData.createPost($ scope.formData)
  };
};


解决方案

模块功能需要依赖作为第二个参数。如果没有相关性可以这样写:

  angular.module('博客',[])

I'm trying to call a function defined in the factory via controller, but getting an error:

TypeError: undefined is not a function.

Could someone help please help me?

//service
angular.module('Blog').factory('postData', function($http) {
  var postData = {}
  postData.data.posts = [{
    title: 'first title',
    contents: 'first contents'
  }]
  postData.loadPosts = function() {
    postData.data.posts = $http.get('./posts.json')
      .success(function(data) {
        postData.data.posts = data;
      })
      .error(function() {
        console.error('Failed loading data');
      })
  }
  postData.createPost = function(newPost) {
    var data;
    if (newPost.newPostTitle === '' || newPost.newPostContents === '') {
      alert('Neither the Title nor the Body are allowed to be left blank.');
      return false;
    }
    data = {
      new_post: {
        title: newPost.newPostTitle,
        contents: newPost.newPostContents
      }
    };
    $http.post('./posts.json', data).success(function(data) {
      postData.data.posts.push(data);
      console.log('Successfully created post.');
    }).error(function() {
      console.error('Failed to create new post.');
    });
    return true;
  };

  console.log("initialized postdata");
  return postData;
})


//controller
var createPostCtrl = function($scope, $location, postData) {
  postData = postData;
  $scope.data = postData.data;
  postData.loadPosts();
  $scope.formData = {
    newPostTitle: '',
    newPostContents: ''
  };


  $scope.createPost = function() {
    // This one is throwing an error TypeError: undefined is not a function
    postData.createPost($scope.formData)
  };
};

解决方案

module function need dependency as second argument. If no dependency you can write:

angular.module('Blog', [])

这篇关于"类型错误:未定义不是一个函数"调用从工厂函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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