2014-01-19 33 views
0

這應該是很簡單,但由於某些原因,當我有Angular.js子輸入元素沒有得到父母範圍

<div class="file-navigator" ng-controller="FileSystemCtrl"> 
     <input type="file" id="openFile" ng-model="path" ng-change="openFolder()" nwdirectory /> 

ng-change不會被觸發。 如果我使用

onchange="angular.element(this).parent().scope().openFolder()" 

onchange事件被觸發,但很明顯,這是醜陋的。

FileSystemCtrl被定義爲一個模塊,我正在導入到我的應用程序中,它的結構像這樣。

angular.module('myApp.FileSystemModule',[]) 
.factory('FileSystemModel',function($rootscope){...}) 
.controller('FileSystemCtrl',function(){...}); 

任何想法爲什麼孩子不知道它的父母控制器?特別是因爲孩子沒有自己的控制器?

回答

1

AngularJs不支持輸入類型文件。見this issue。和this。你的onchange事件現在是最好的選擇。

1

另一種方法是使用的$compile一個directive趁着有ng-model互動:

.directive('path', function($compile) { 
    return { 
     restrict: 'E',//<path></path> in your markup 
     replace: true, 
     template: '<input type="file" />', 
     link: function(scope, elem, attrs) { 
       var textField = $(elem).attr('ng-model', 'something'); 

       $compile(textField)(scope); 

       $(elem).change(function() { 
         //do stuff 
       }); 

     } 
    }; 
}); 

我沒有測試它,但它提供了一個得到去。

+0

感謝羅蘭,我知道我必須瞭解其他一些事情的編譯方法。我可以在這裏玩。 – pedalpete