加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS依赖注入交换实现

发布时间:2020-12-17 08:32:37 所属栏目:安全 来源:网络整理
导读:我还在学习AngularJS,并且对他们依赖注入的风格有疑问.例如,假设我有一个DataProcessor服务,它有一个processData方法,它接受一个uri参数,它需要读取该数据(可能是xml,json等),然后对它执行一些操作. DataProcessor构造函数接受DataReader接口的实现,该接口知
我还在学习AngularJS,并且对他们依赖注入的风格有疑问.例如,假设我有一个DataProcessor服务,它有一个processData方法,它接受一个uri参数,它需要读取该数据(可能是xml,json等),然后对它执行一些操作. DataProcessor构造函数接受DataReader接口的实现,该接口知道如何读取某种文件类型.以下是我所说的一些示例服务:
// implementations of the DataReader interface
myApp.service('XmlDataReader',function() {
    this.readData = function(uri) {
        // read xml data from uri
    }
}]);

myApp.service('JsonDataReader',function() {
    this.readData = function(uri) {
        // read json data from uri
    }
}]);

// data processing service that takes in an implementation of a DataReader
myApp.service('DataProcessor',['DataReader',function(DataReader) {

    this.processData = function(uri) {
        var readData = DataReader.readData(uri);

        // process data and return it
    }
}]);

从典型的依赖注入角度来看,可以将特定类型的DataReader传递到DataProcessor中并像这样使用:

var dataProcessor = new DataProcessor(new JsonDataReader());
var processedData = dataProcessor.processData('dataz.json');

AngularJS的做法是什么?

这是一篇关于这个主题的好文章 http://phonegap-tips.com/articles/conditional-dependency-injection-with-angularjs.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读