And*_*erj 24 templates angularjs angularjs-directive
我尝试写一个非常灵活的指令.为此,我希望用户定义在返回的一部分中使用的模板(如ui-bootstrap typeahead指令中所示).
所以我定义我的模板如下:
<script type="text/ng-template" id="myDirectivesCustomTemplate.html">
<ul>
<li ng-repeat="value in values">
<a ng-click="doSomething(value.id)">
{{value.name}}
</a>
</li>
</ul>
</script>
Run Code Online (Sandbox Code Playgroud)
我在我的指令中设置了这个模板
<div
my-directive
my-directive-custom-template="myDirectivesCustomTemplate.html"
my-directive-data="someScopeData">
Run Code Online (Sandbox Code Playgroud)
现在在我的指令中,如何呈现自定义模板并将其与传递的数据一起使用?当我尝试使用它直接返回模板时,它会抛出一个ReferenceError: $scope is not defined错误.如果我在没有范围的情况下调用它,则表示ReferenceError: myDirectiveCustomTemplate is not defined错误.
如果我不想直接使用它作为回报,我在哪里以及如何使用我的模板?
编辑:让我们说,这是我的指示:
(function() {
'use strict';
var Combobox = function() {
var displayInputField = elem.find('input.dropdown');
scope.$watch(scope.nsdComboboxModel,function(newVal){
/* search for newVal in given data object */
scope.setDisplayInputValue(newVal);
});
scope.setDisplayInputValue = function(value)
{
displayInputField.val(value);
};
scope.elementSelected = function (item, model, label) {
scope.ComboboxCallback(item);
scope.setDisplayInputValue(label);
};
}
return {
restrict: 'A',
transclude: true,
scope: {
Combobox: '@', /* used as HTML/CSS-id/name/path */
ComboboxModel: '=', /* the actual AngularJS model (ng-model) */
ComboboxAutocompleteData: '=', /* the data used for autoComplete (must be array of objects having id and value) */
ComboboxDropdownData: '=', /* data used by the dropdown template */
ComboboxCallback: '=', /* a callback function called with selected autocomplete data item on select */
ComboboxLabel: '@', /* label for the input field */
ComboboxDropdownTemplate: '@' /* label for the input field */
},
template:
'<section class="-combobox-directive container-fluid">' +
'<label for="{{Combobox}}" ng-if="ComboboxTranslation" translate="{{ComboboxLabel}}"></label>' +
'<div class="combobox input-group">' +
'<input type="text" ' +
'id="{{Combobox}}" ' +
'autocomplete="off" ' +
'ng-model="ComboboxDestinationDisplay" ' +
'data-toggle="dropdown" ' +
'typeahead="value as location.value for location in ComboboxAutocompleteData | filter:$viewValue" ' +
'typeahead-editable="false" ' +
'typeahead-on-select="elementSelected($item, $model, $label)" ' +
'class="form-control dropdown">' + // dropdown-toggle
'<span data-toggle="dropdown" class="input-group-addon dropdown-toggle">' +
'<span class="glyphicon glyphicon-globe"></span>' +
'</span>' +
//$compile(ComboboxDropdownTemplate) +
'</div>' +
'</section>',
link: link
};
};
angular.module('vibe.directives').directive('nsdCombobox', [NsdCombobox]);
})();
Run Code Online (Sandbox Code Playgroud)
Whi*_*her 15
<script type="text/ng-template" id="myDirectivesCustomTemplate.html">
<ul>
<li ng-repeat="value in values">
<a ng-click="doSomething({id:value.id})">
{{value.name}}
</a>
</li>
</ul>
</script>
<div ng-controller="MainCtrl">
<div my-directive template="myDirectivesCustomTemplate.html" mydata="mydata" mycallback="doSomething(id)"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
app.controller('MainCtrl',function($scope){
$scope.mydata = [{id:1,name:'One'},{id:2,name:'Two'},{id:3,name:'Three'}];
$scope.doSomething = function(id){
alert(id);
}
});
app.directive('myDirective', function($templateCache,$compile) {
return {
restrict: 'A',
scope:{
template : "@",
mydata : "=",
mycallback:"&"
},
link: function(scope,element) {
var template = $templateCache.get(scope.template);
scope.values = scope.mydata;
scope.doSomething = scope.mycallback;
element.append($compile(template)(scope));
}
}
});
Run Code Online (Sandbox Code Playgroud)
Cha*_*ani 13
看看你的指令我可以建议试试ng-include.你想去哪里
//$compile(ComboboxDropdownTemplate) +
'</div>'
Run Code Online (Sandbox Code Playgroud)
改为
<span ng-include='templateUrlPropertyOnScope'>
'</div>'
Run Code Online (Sandbox Code Playgroud)
templateUrlPropertyOnScope属性应指向服务器端或使用创建的脚本部分中的模板type=text/ng-template.
我知道这是四年后的事了,但如果有人仍然有这个问题,也许这个答案也可能有用。
使用这样的简单指令:
<my-directive template="custom-template.html"></my-directive>
Run Code Online (Sandbox Code Playgroud)
然后,您可以在指令中引用模板属性,如下所示:
(function() {
angular
.module('app')
.directive('myDirective', myDirective);
function myDirective() {
return {
restrict: 'E',
templateUrl: function(elem, attrs) {
return attrs.template;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)