Angularjs-创建“工厂”的正确方法是什么?如何使用它们?

Angularjs-创建“工厂”的正确方法是什么?如何使用它们?,angularjs,Angularjs,我是一个非常新的角度,我想了解工厂的方法 我试图用不同的方法为我的应用程序创建一个工厂方法。但对我来说什么都不管用——为什么 还有,创建工厂方法的正确方法是什么?我们有多种方法来创建工厂方法吗如果是,请说明它们的优点和缺点 以下是我的尝试: var app = angular.module('MyApp', ['authService' ]); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; });

我是一个非常新的角度,我想了解工厂的方法

我试图用不同的方法为我的应用程序创建一个工厂方法。但对我来说什么都不管用——为什么

还有,创建工厂方法的正确方法是什么?我们有多种方法来创建工厂方法吗如果是,请说明它们的优点和缺点

以下是我的尝试:

var app = angular.module('MyApp', ['authService' ]);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

//type 1
app.factory('appFactory', function(){
  console.log(" called from app factory")
})

//type 2
angular.module('MyApp')
.factory( 'myFactory', function(){

  console.log("called from factory");

})

//type 3
angular.module("authService", [])

.factory("Auth", function( $http, $q, AuthToken ) {

  console.log(" i am called from auth factory")


})

这里是你能找到的最好的例子


这里是你能找到的最好的例子


使用工厂,您可以创建对象。可以帮助您开始的一个简单示例如下:

angular
.module('myApp', [])
.factory('Post', ['$http', function($http) {
    var service = {
        all: all,
        get: get
    };

    return service;

    function all() {
        return $http
            .get('api/posts/')
            .then(function(response){
                return response.data;
            });
        }
    }

    function get(id) {
        return $http
            .get('api/posts/' + id);
            .then(function(response){
                return response.data;
            });
        }
    }
}])
.controller('PostsController', ['Post', function(Post){
    var self = this;
    self.posts = Posts.get();
}]);

所以检查一下,我可以简单地将它注入我的控制器,然后轻松地调用它的方法。这样就有了关注点的分离,您可以使所有这些逻辑远离控制器和组件。有一篇关于工厂和服务的文章,内容非常精彩。

通过工厂,您可以创建对象。可以帮助您开始的一个简单示例如下:

angular
.module('myApp', [])
.factory('Post', ['$http', function($http) {
    var service = {
        all: all,
        get: get
    };

    return service;

    function all() {
        return $http
            .get('api/posts/')
            .then(function(response){
                return response.data;
            });
        }
    }

    function get(id) {
        return $http
            .get('api/posts/' + id);
            .then(function(response){
                return response.data;
            });
        }
    }
}])
.controller('PostsController', ['Post', function(Post){
    var self = this;
    self.posts = Posts.get();
}]);
所以检查一下,我可以简单地将它注入我的控制器,然后轻松地调用它的方法。这样就有了关注点的分离,您可以使所有这些逻辑远离控制器和组件。还有一篇关于工厂和服务的文章

这是你的博客

  • 你们工厂必须退货。查看plunker中的return语句
  • 您应该将工厂注入控制器
    MainCtrl
    ,以便初始化工厂。这就是为什么没有打印
    console.log
    s
工厂/服务的最佳利用

根据下面广泛使用的方法,这是最佳方法。请阅读更多有关上的角度指南

这是你的战利品

  • 你们工厂必须退货。查看plunker中的return语句
  • 您应该将工厂注入控制器
    MainCtrl
    ,以便初始化工厂。这就是为什么没有打印
    console.log
    s
工厂/服务的最佳利用

根据下面广泛使用的方法,这是最佳方法。请阅读更多有关上的角度指南


工厂用于创建服务。服务是java脚本函数,一旦我们定义了工厂,我们就可以在应用程序中的任何地方使用它。 例如:

<div ng-app="myApp" ng-controller="Ctrl">
<h3>{{hello}}</h3>
</div>
<script>
var app = angular.module('myApp', []);
app.factory("test",function(){
    var fact={}     //Creating Object
    fact.love=function(){   //Access love method through the fact object
        var serve="This is test for  factory";
        return serve;
    }
    return fact;          //return object
})
app.controller('Ctrl', function($scope, test) {
    $scope.hello=test.love(); //assign the test factory function in ctrler
});
</script>

{{你好}}
var-app=angular.module('myApp',[]);
应用程序工厂(“测试”,功能(){
var fact={}//正在创建对象
fact.love=function(){//通过事实对象访问love方法
var serve=“这是工厂测试”;
回击发球;
}
返回事实;//返回对象
})
应用控制器('Ctrl',函数($scope,test){
$scope.hello=test.love();//在ctrler中分配测试工厂函数
});

工厂用于创建服务。服务是java脚本函数,一旦我们定义了工厂,我们就可以在应用程序中的任何地方使用它。 例如:

<div ng-app="myApp" ng-controller="Ctrl">
<h3>{{hello}}</h3>
</div>
<script>
var app = angular.module('myApp', []);
app.factory("test",function(){
    var fact={}     //Creating Object
    fact.love=function(){   //Access love method through the fact object
        var serve="This is test for  factory";
        return serve;
    }
    return fact;          //return object
})
app.controller('Ctrl', function($scope, test) {
    $scope.hello=test.love(); //assign the test factory function in ctrler
});
</script>

{{你好}}
var-app=angular.module('myApp',[]);
应用程序工厂(“测试”,功能(){
var fact={}//正在创建对象
fact.love=function(){//通过事实对象访问love方法
var serve=“这是工厂测试”;
回击发球;
}
返回事实;//返回对象
})
应用控制器('Ctrl',函数($scope,test){
$scope.hello=test.love();//在ctrler中分配测试工厂函数
});