Angular.js directive dynamic templateURL
我在
1 | <hymn ver="before-{{ week }}-{{ day }}"></hymn> |
这首赞美诗有多个版本,都是根据它是哪一周哪一天写的。我预计将使用该指令填充正确的
1 2 3 4 5 6 7 8 9 10 11 12 | emanuel.directive('hymn', function() { var contentUrl; return { restrict: 'E', link: function(scope, element, attrs) { // concatenating the directory to the ver attr to select the correct excerpt for the day contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html'; }, // passing in contentUrl variable templateUrl: contentUrl } }); |
excerpts目录中有多个文件标记为
1 2 3 4 5 6 7 8 9 10 11 | emanuel.directive('hymn', function() { return { restrict: 'E', link: function(scope, element, attrs) { // some ode }, templateUrl: function(elem,attrs) { return attrs.templateUrl || 'some/path/default.html' } } }); |
因此,您可以通过标记提供模板URL
1 | <hymn template-url="contentUrl"><hymn> |
现在,您只需注意属性ContentURL使用动态生成的路径填充。
您可以使用
尝试如下操作:
1 2 3 4 5 6 7 8 9 10 11 | emanuel.directive('hymn', function() { return { restrict: 'E', link: function(scope, element, attrs) { scope.getContentUrl = function() { return 'content/excerpts/hymn-' + attrs.ver + '.html'; } }, template: '' } }); |
UPD。用于观察
1 2 3 4 5 6 7 8 9 10 11 12 | emanuel.directive('hymn', function() { return { restrict: 'E', link: function(scope, element, attrs) { scope.contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html'; attrs.$observe("ver",function(v){ scope.contentUrl = 'content/excerpts/hymn-' + v + '.html'; }); }, template: '' } }); |
多亏了@pgregory,我可以使用这个指令进行内联编辑来解决我的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | .directive("superEdit", function($compile){ return{ link: function(scope, element, attrs){ var colName = attrs["superEdit"]; alert(colName); scope.getContentUrl = function() { if (colName == 'Something') { return 'app/correction/templates/lov-edit.html'; }else { return 'app/correction/templates/simple-edit.html'; } } var template = ''; var linkFn = $compile(template); var content = linkFn(scope); element.append(content); } } }) |
这里不需要自定义指令。只需使用ng include src属性。它是编译好的,所以你可以把代码放进去。有关您的问题,请参阅Pluker with Solution。
1 | <ng-include src="'content/before-'+ week + '-' + day + '.html'"></ng-include> |
我也有同样的问题,我的解决方式和其他人略有不同。我用的是角1.4.4。
在我的例子中,我有一个shell模板来创建一个CSS引导面板:
1 2 3 4 | <h3 class="panel-title">{{title}} <sp-panel-body panelbodytpl="{{panelbodytpl}}"></sp-panel-body> |
我想根据路线包括面板主体模板。
1 2 3 4 5 6 7 8 9 10 11 | angular.module('MyApp') .directive('spPanelBody', ['$compile', function($compile){ return { restrict : 'E', scope : true, link: function (scope, element, attrs) { scope.data = angular.fromJson(scope.data); element.append($compile('<ng-include src="\'' + scope.panelbodytpl + '\'"></ng-include>')(scope)); } } }]); |
当路由为
1 2 3 4 5 | <sp-panel title="{{student.firstName}} {{student.middleName}} {{student.lastName}}" panelbodytpl="{{'/student/panel-body.html'}}" data="{{student}}" ></sp-panel> |
panel-body.html模板如下:
1 | Date of Birth: {{data.dob * 1000 | date : 'dd MMM yyyy'}} |
示例数据,以防有人想尝试:
1 2 3 4 5 6 7 8 | var student = { 'id' : 1, 'firstName' : 'John', 'middleName' : '', 'lastName' : 'Smith', 'dob' : 1130799600, 'current-class' : 5 } |
我有一个例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!DOCTYPE html> <html ng-app="app"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <h4>Register Form</h4> <form class="form-horizontal" ng-submit="" name="f" novalidate> <label>{{item.Label}}</label> <element type="{{item.Type}}" model="item"></element> <input ng-show="f.$valid" type="submit" id="submit" value="Submit" class="" /> </form> <script src="https://code.jquery.com/jquery-1.10.2.min.js"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"> <script src="app.js"> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | angular.module('app', []) .controller('formView', function ($scope) { $scope.elements = [{ "Id":1, "Type":"textbox", "FormId":24, "Label":"Name", "PlaceHolder":"Place Holder Text", "Max":20, "Required":false, "Options":null, "SelectedOption":null }, { "Id":2, "Type":"textarea", "FormId":24, "Label":"AD2", "PlaceHolder":"Place Holder Text", "Max":20, "Required":true, "Options":null, "SelectedOption":null }]; }) .directive('element', function () { return { restrict: 'E', link: function (scope, element, attrs) { scope.contentUrl = attrs.type + '.html'; attrs.$observe("ver", function (v) { scope.contentUrl = v + '.html'; }); }, template: '' } }) |