2017-03-07 63 views
0

我有一個自定義指令,用於檢測類型文件的輸入何時發生更改。發生這種情況時,我想查找另一個輸入並更改其ng-model。我似乎無法讓模型更改被觸發。我做了研究,找不到答案。這是我引用here當從文件選擇指令更改值時更新ng-model

//my controller 
 
$scope.headShotUpload = function (event) { 
 

 
     var path = '', 
 
      id = event.target.id.toString(), 
 
      files = event.target.files, 
 
      tempString = ''; 
 
     if(id.includes('nfl')){ 
 
      path = $scope.filePaths.headShot.nfl +'/'+ files[0].name; 
 
      tempString = id.replace("nfl-",""); 
 

 
      var labelInputId = tempString+'-path' 
 
      var input = $(labelInputId); 
 

 
      input.val(path); 
 
      input.trigger('input'); 
 

 
     } 
 

 
    }; 
 
    
 
    //my directive 
 
    
 
    angular.module('app') 
 
    .directive('customOnChange', function() { 
 
     return { 
 
      restrict: 'A', 
 
      link: function (scope, element, attrs) { 
 
       var onChangeHandler = scope.$eval(attrs.customOnChange); 
 
       element.bind('change', onChangeHandler); 
 
      } 
 
     }; 
 
    });
<td class="rcs-table-btn"> 
 
    <span > 
 
    <label class="btn btn-default btn-file btn-rcsorange" >NFL<input type="file"   style="display: none;" id="offense-top-header1-headshot-nfl-1" custom-on-change="headShotUpload"></label> 
 
    </span> 
 
<span> 
 
    <label class="btn btn-default btn-file btn-rcsorange" >NCAA <input type="file" style="display: none;" id="offense-top-header1-headshot-ncaa-1" custom-on-change="headShotUpload"></label> 
 
    </span> 
 
</td> 
 
<td class="rcs-table-input"> 
 
<input type="text" class="rcs-input-table" id="offense-top-header1-headshot-1-path" placeholder="(path)" ng-model="page.offense.list[player.position].header1[0].headShot"> 
 
</td>

+0

你好,如果任何人碰到這個問題我來解決它通過更換通過document.getElementById()的jQuery($)和它的工作。出於某種原因,jquery不適合我。我不得不尋找原因。 – inhaler

回答

0

指令在AngularJS應用程序訪問父範圍由默認的問題。您可以嘗試使用$ parent來設置父級作用域,而且父級作用域也必須是對象而不是單個命名作用域。您不能直接命名變量,如scope.parentModel = newName,因爲它是原始類型(即字符串,數字或布爾類型),並且「寫入」始終轉到本地作用域/對象。對對象屬性的任何寫入(無論是來自父對象還是子對象)都將轉到該對象。

angular.module('app') 
.directive('customOnChange', function() { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      var onChangeHandler = function() { 
       scope.$eval(attrs.customOnChange); 
       scope.$parent.obj.parentModel = 'newVal'; 
      } 
      element.bind('change', onChangeHandler()); 
     } 
    }; 
}); 
相關問題