如何在angularjs中注入组件

时间:2014-12-22 19:41:16

标签: javascript angularjs

我使用meanjs(meanjs.org)样板代码构建我的网站,我想在网站上实现文件上传功能。

我正在使用https://github.com/ghostbar/angular-file-model并安装了该组件并添加到bower.json中。如何将其注入我的应用程序

以下是网站上的用法

使用凉亭安装:

bower install angular-file-model --save

添加到您的HTML文件:

<script src='/bower_components/angular-file-model/angular-file-model.js'></script>

现在,请注入您的应用程序:

angular.module('myApp', ['file-model']);

如何在我的应用中注入?

angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
    function($scope, $stateParams, $location, Authentication, Articles) {
        $scope.authentication = Authentication;

        $scope.file = null;

        $scope.$watch('file', function (newVal) {
            if (newVal)             console.log(newVal);
        });

        $scope.create = function() {
            var article = new Articles({
                title: this.title,
                content: this.content
            });
            article.$save(function(response) {
                $location.path('articles/' + response._id);

                $scope.title = '';
                $scope.content = '';
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };

        $scope.remove = function(article) {
            if (article) {
                article.$remove();

                for (var i in $scope.articles) {
                    if ($scope.articles[i] === article) {
                        $scope.articles.splice(i, 1);
                    }
                }
            } else {
                $scope.article.$remove(function() {
                    $location.path('articles');
                });
            }
        };

        $scope.update = function() {
            var article = $scope.article;

            article.$update(function() {
                $location.path('articles/' + article._id);
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };

        $scope.find = function() {
            $scope.articles = Articles.query();
        };

        $scope.findOne = function() {
            $scope.article = Articles.get({
                articleId: $stateParams.articleId
            });
        };
    }
]);

是我的控制器页面。哪个可以在https://github.com/meanjs/mean

找到

我的观看页面是

<section data-ng-controller="ArticlesController" data-ng-init="find()">
    <div class="page-header">
        <h1>Articles</h1>
    </div>
    <form>
        <input type='file' file-model='file'>
    </form>

</section>

0 个答案:

没有答案