AngularJS 1.5 scope in directive ng-include -


here's code http://jsfiddle.net/miuosh/n6yppypj/ uploadfile directive. problem use this

  <input type="file" file-model="myfile" />   <button ng-click="uploadfile()">upload me</button> 

in ng-include="'views/uploadfileform.html'".

in directive add "myfile" scope. turns out angular create new scope myfile. add "myfile" "rootscope" need use

modelsetter(scope.$parent.$parent.$parent[..],element[0].files[0]) 

which inconvenient because need know how many parent scope have.

i have faced similar problem dealing file input angular. can create directive listen file change , call controller function file. here jsfiddle it.

var app = angular.module('app', []);  app.controller('yourctrl', function() {     $scope.image;     $scope.imagechanged= function (files) {         $scope.image = files;     }; });  app.directive('customonchange', function() {   return {     restrict: 'a',     link: function (scope, element, attrs) {       var onchangefunc = scope.$eval(attrs.customonchange);       element.bind('change', function(event){         var files = event.target.files;         onchangefunc(files);       });        element.bind('click', function(){         element.val('');       });     }   }; })  <input type="file" id="imginput" custom-on-change="imagechanged"/>  

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -