Search code examples
javascriptangularjsng-file-upload

AngularJs multiple controllers of the same name in one file isolate scope


At the beginning I will say that I am a beginner in angular and i learning it. I planing create gallery system where user can create multiple galleries. I was using directive what http.get parital compile and create form for drop images. For one form working nice but when i add more form my function execute multipile times. I think my logic is bad for this or i doing something bad. This is my directive.

directive("contentControl",function($http,$compile){
    return {
        template: 'Click here',
        link: function(scope, element, attrs) {
            element.bind("click", function () {
                $http.get("/js/angular/File/view/admin/FileUpload.html").then(function(resp) {
                    var html = resp.data;
                    var templateGoesHere = angular.element(document.getElementById('templateGoesHere'));
                    templateGoesHere.append(html);

                    $compile(templateGoesHere)(scope);

                });

            });
        },

    }

Template

<div ng-controller="FileController">
    <button ng-click="upload(files)"></button>
    <form name="myForm">
        <div ngf-drop="add($files)" ng-model="files" class="drop-box clearfix"
             ngf-drag-over-class="dragover" ngf-multiple="true">
            <div ng-repeat="image in images">
                <div class="col-sm-3">
                    <img ngf-thumbnail="!image.file.$error && image.file"
                         ngf-size="{width: 5024, height:200, quality: 0.9}"
                         class="img-responsive">
                </div>
            </div>
        </div>
    </form>
</div>

And controller

.controller('FileController', function ($scope, Upload) {
        $scope.files = [];
        $scope.images = [];
        $scope.add=function(files)
        {
            console.log(files);
        }
        $scope.upload = function (files) {
            angular.forEach($scope.images,function(file)
            {
        });
            //angular.forEach($scope.images,function(file)
            //{
            //    console.log(file.file);
            //    Upload.upload({
            //        url: '/gallery/create',
            //        fields: {'name': file.file.name}, // additional data to send
            //        file: file.file
            //    });
            //});
        }
    });

I looking for isolate data or some one can explain me better logic for create multipile galleries


Solution

  • This, is normal, because each time you use the directive a new http.get will be made.

    1. Use tempalteUrl to get the tempalte. or,
    2. Remove this directive, and use ngInclude & ngIf to include your template when clicking on element.

      <a href="#" ng-click="showMyTemplate()">Click here</a>
      <div ng-if="showTmp">
         <div ng-include="template.html"/>
      </div>
      

    In the controller

    // init
    $scope.showTmp = false;
    $scope.showMytemplate = function(){
        $scope.showTmp = true;
    }