与AngularJS面向对象的方法 [英] Object oriented approach with AngularJS

查看:1105
本文介绍了与AngularJS面向对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎角不提供内建的溶液来定义与属性和方法类实例和它的最多显影剂建立这种

It seems that Angular does not provide a built-in solution to define class instances with properties and methods and that it's up the developer to build this.

什么是您认为要做到这一点的最佳做法?
如何你与后端链接此?

What is the best practice to do this in your opinion? How to you link this with the backend?

一些我收集了使用厂家的服务和命名功能的提示。

Some of the tips I have gathered use factory services and named functions.

来源:
<一href=\"https://medium.com/opinionated-angularjs/angular-model-objects-with-javascript-classes-2e6a067c73bc\"相对=nofollow>政党成员1
政党成员2

感谢您的见解。

推荐答案

我认为最接近结构对象它可能是一个工厂,有以下几个原因:

I think that the closest structure to an Object it's probably a factory, for several reasons:

基本语法:

.factory('myFactory', function (anInjectable) {

  // This can be seen as a private function, since cannot
  // be accessed from outside of the factory 
  var privateFunction = function (data) {
    // do something 
    return data
  }

  // Here you can have some logic that will be run when 
  // you instantiate the factory
  var somethingUseful = anInjectable.get()
  var newThing = privateFunction(somethingUseful)

  // Here starts your public APIs (public methods)
  return {
    iAmTrue: function () {
      return true
    },

    iAmFalse: function () {
      return false
    },

    iAmConfused: function () {
      return null
    }
  }
})

然后你就可以使用它像一个标准的对象:

And then you can use it like a standard Object:

var obj = new myFactory()

// This will of course print 'true'
console.log( obj.iAmTrue() )

希望这可以帮助,我完全知道,与棱角分明的模块的第一个影响是pretty激烈...

Hope this helps, I perfectly know that the first impact with angular modules can be pretty intense...

这篇关于与AngularJS面向对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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