gpt4 book ai didi

angularjs - 如何在 AngularJS 指令中使用定义的文本/ng-模板

转载 作者:行者123 更新时间:2023-12-02 22:01:10 24 4
gpt4 key购买 nike

我尝试编写一个非常灵活的指令。为此,我希望用户定义一个在我的返回部分使用的模板(如 ui-bootstrap typeahead directive 中所示)。

所以我这样定义我的模板:

<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>

我在我的指令中设置了这个模板

<div 
my-directive
my-directive-custom-template="myDirectivesCustomTemplate.html"
my-directive-data="someScopeData">

现在在我的指令中,如何呈现自定义模板并将其与传递的数据一起使用?当我尝试使用它直接在模板中返回时,它会抛出 ReferenceError: $scope is not Defined 错误。如果我在没有范围的情况下调用它,它会显示ReferenceError:myDirectiveCustomTemplate未定义错误。

如果我不想直接将其用作返回,我可以在哪里以及如何使用我的模板?

编辑:比方说,这是我的指令:

(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]);
})();

最佳答案

HTML

<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>

JS

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));
}
}
});

关于angularjs - 如何在 AngularJS 指令中使用定义的文本/ng-模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com