2015-12-04 116 views
0

我在anguarjs中編寫了一個指令並嘗試在html頁面中使用該指令。Angular指令無法加載

directive.js

.directive('formFieldNew', function($timeout, FieldTypes) { 
return { 
    restrict : 'EA', 
    templateUrl : '/common-licensing/resources/partials/subscriber/template.html', 
    replace : true, 
    scope : { 
     record : '=', 
     field : '@', 
     live : '@', 
     required : '@' 
    }, 
    link : function($scope, element, attr) { 
     $scope.$on('record:invalid', function() { 
      $scope[$scope.field].$setDirty(); 
     }); 

     $scope.types = FieldTypes; 

     $scope.remove = function(field) { 
      delete $scope.record[field]; 
      $scope.blurUpdate(); 
     }; 

     $scope.blurUpdate = function() { 
      if ($scope.live !== 'false') { 
       $scope.record.$update(function(updatedRecord) { 
        $scope.record = updatedRecord; 
       }); 
      } 
     }; 
     var saveTimeout; 
     $scope.update = function() { 
      $timeout.cancel(saveTimeout); 
      saveTimeout = $timeout($scope.blurUpdate, 1000); 
     }; 
    } 
}; 

new.html

<form name='newContact' novalidate class='form-horizontal'> 
<form-field-new ng-repeat='(k,v) in productTempDetailsLists' record='productTempDetailsLists' field='{{k}}'></form-field-new> 
<button class='btn btn-primary' ng-click='save()' ng-disabled='newContact.$invalid'> Create Contact </button> 

</form> 

template.html

<div> 
<input ng-model='record[field][0]' type='{{record[field][1]}}' class='form control'/> 
</div> 

當我運行該文件,我摹在我的控制檯中設置下面的錯誤。

Error: [$compile:tplrt] http://errors.angularjs.org/1.3.15/$compile/tplrt?p0=formFieldNew&p1=%2Fcommon-licensing%2Fresources%2Fpartials%2Fsubscriber%2Ftemplate.html 
    at Error (native) 
    at http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:6:417 
    at http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:65:275 
    at http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:112:113 
    at n.$eval (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:126:15) 
    at n.$digest (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:123:106) 
    at n.$apply (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:126:293) 
    at l (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:81:240) 
    at M (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:85:342) 
    at XMLHttpRequest.F.onload (http://localhost:8080/common-licensing/resources/lib/angular/angular.min.js:86:367) 

我嘗試添加了<div>標籤爲我new.html文件按照錯誤日誌,但沒有奏效。請幫助我。

+0

這樣看來,那'templateUrl'是不是一個有效的路徑。這個HTML真的存在於一個名爲'template.js'的文件中嗎?或者是一個錯字?在其他地方,該文件被稱爲'template.html' – Claies

+0

我正在爲我試過的其他templateUrl路徑收到404(Not Found)錯誤。此路徑沒有顯示任何此類錯誤,因此我認爲這是正確的路徑。 –

+0

預測未來的問題,您正在創建一個無法與Form控制器通信的隔離範圍。某些內置功能不會以這種方式工作。你可能需要:FormController,但這可能會變得棘手。我只會使用'scope:true' –

回答

0

我有類似的問題,其中templateURL不適用於絕對URL。看到這個檢查了這一點,其中,你會發現同樣的錯誤http://plnkr.co/edit/NoSZjfIiCiRrCaBbsHiE?p=preview

使用相對的URL和嘗試以下

.directive('formFieldNew', function($timeout, FieldTypes) { 
return { 
    restrict : 'E', 
    //Not sure if this is correct relative path. check 
    templateUrl : '../common-licensing/resources/partials/subscriber/template.html', 
    replace : true, 
    scope : { 
     record : '=', 
     field : '@', 
     live : '@', 
     required : '@' 
    }, 
    link : function($scope, element, attr) { 
     $scope.$on('record:invalid', function() { 
      $scope[$scope.field].$setDirty(); 
     }); 

     $scope.types = FieldTypes; 

     $scope.remove = function(field) { 
      delete $scope.record[field]; 
      $scope.blurUpdate(); 
     }; 

     $scope.blurUpdate = function() { 
      if ($scope.live !== 'false') { 
       $scope.record.$update(function(updatedRecord) { 
        $scope.record = updatedRecord; 
       }); 
      } 
     }; 
     var saveTimeout; 
     $scope.update = function() { 
      $timeout.cancel(saveTimeout); 
      saveTimeout = $timeout($scope.blurUpdate, 1000); 
     }; 
    } 
};