[英]Angular JS Directive loaded in expression
I'm new to Angular, and have found a ton of resources about directives and nesting, but can't seem to get this simple example to work. 我是Angular的新手,已经找到了大量关于指令和嵌套的资源,但似乎无法让这个简单的例子起作用。 So basically I am working on a tabset, I have an HTML template:
所以基本上我正在制作一个tabset,我有一个HTML模板:
tabset.html
<div>
<ul>
<li ng-repeat="tab in tabset.tabs" ng-class="{active:tabset.current()==$index}">
<a href ng-click="tabset.current($index)">{{tab}}</a>
</li>
</ul>
<div>
<div ng-repeat="pane in tabset.panes">
<div ng-show="tabset.current()==$index">
{{pane.contents}}
</div>
</div>
</div>
</div>
And a search form template: 和搜索表单模板:
search-form.html
<div>
<form name="ytSearch" ng-submit="YTCtrl.submit()" novalidate>
<label for="search_box">Search For: </label>
<input id="search_box" ng-model="YTCtrl.searchString"/>
<br>
<label for="location">Location: </label>
<input id="location" ng-model="YTCtrl.location"/>
within
<input type="numeric" value="100" ng-model="YTCtrl.locationRadius" />
<select ng-model="YTCtrl.locationUnit">
<option value="ft">Feet</option>
<option value="m">Meters</option>
<option value="mi">Miles</option>
<option value="km">Kilometers</option>
</select>
<br>
<label for="search_order">Sort By: </label>
<select id="search_order" ng-model="YTCtrl.order">
<option value="relevance">Relevance</option>
<option value="date">Date</option>
<option value="rating">Rating</option>
</select>
<br>
<button id="search">
Search
</button>
</form>
</div>
And a simple app file with 2 directives to handle each of the templates: 还有一个简单的app文件,带有2个指令来处理每个模板:
app.js
(function() {
angular.module("JHYT", [])
.directive("sidebarTabset", function($compile) {
return {
restrict : 'E',
templateUrl : 'tabset.html',
controller : function($scope, $compile, $http) {
this._current = 0;
this.current = function(i) {
if (i != null)
this._current = i;
return this._current;
};
this.tabs = ['Search', 'Favorite'];
this.panes = [{
contents : "<search-form></search-form>"
}, {
contents : "Favorite Pane"
}];
},
controllerAs : 'tabset',
};
}).
directive("searchForm", function() {
return {
restrict : 'E',
templateUrl : 'search-form.html',
controller : function($scope, $compile, $http) {
this.searchString = '';
this.location = '';
this.locationRadius = '';
this.locationUnit = 'mi';
this.order = 'relevance';
this.submit = function() {
console.log("Submit");
};
},
controllerAs : 'YTCtrl',
}
});
})();
So as you can probably tell, the idea is to be able to send a JSON object into the tabset (through a service probably) and have it build out a dynamic tabset, that actually works exactly as I expected it to. 正如您可能已经知道的那样,我们的想法是能够将JSON对象发送到tabset(可能通过服务),并让它构建一个动态tabset,它实际上与我预期的完全相同。 What isn't working is that in the first tab, the content, which is
<search-form></search-form>
is not processed, and the tag is rendered as plain text in the content area. 不起作用的是,在第一个选项卡中,不处理
<search-form></search-form>
内容,并且标记在内容区域中呈现为纯文本。
Since this is a tabset, the "child" doesn't need anything from the "parent", the search form and the tab itself have no scope dependencies. 由于这是一个tabset,“child”不需要“parent”中的任何内容,搜索表单和选项卡本身没有作用域依赖性。 I tried playing with the link and compile functions after seeing some examples of nested structures, but can't seem tog et them to work.
在看到嵌套结构的一些例子后,我尝试使用链接和编译函数,但似乎无法使用它们。
How can I process the content of that variable so that element directives are rendered using their templates? 如何处理该变量的内容,以便使用其模板呈现元素指令?
EDIT: 编辑:
@sielakos Gave me exactly what I was hoping for, a reusable method for doing this. @sielakos给了我正是我希望的东西,这是一个可重复使用的方法。
I added a directive to my module called compile, which adds a wrapper to allow me to use plain text: 我在我的模块中添加了一个名为compile的指令,它添加了一个包装器以允许我使用纯文本:
.directive("compile", function($compile){
return {
restrict: 'A',
link: function(scope, element, attr){
attr.$observe("compile", function(str){
var compiled = $compile("<div>"+str+"</div>")(scope);
jQuery(element).replaceWith(compiled);
})
}
}
})
And I changed my tabset to use this directive: 我更改了我的tabset以使用此指令:
<div>
<ul>
<li ng-repeat="tab in tabset.tabs" ng-class="{active:tabset.current()==$index}">
<a href ng-click="tabset.current($index)">{{tab}}</a>
</li>
</ul>
<div>
<div ng-repeat="pane in tabset.panes">
<div ng-show="tabset.current()==$index">
<div compile="{{pane.contents}}"></div>
</div>
</div>
</div>
</div>
You will need to compile your string using $compile service if you wish to use it as you would use template. 如果您希望像使用模板一样使用它,则需要使用$ compile服务编译字符串。 Otherwise it will be treated as normal string and displayed as it is.
否则,它将被视为普通字符串并按原样显示。
Here is example how to use it inside directive: 以下是如何在指令中使用它的示例:
var compiled = $compile(str)(scope);
element.empty();
element.append(compiled);
If you wish you can look at this fiddle for more complex example: https://jsfiddle.net/x78uuwp2/ 如果你希望你能看到这个小提琴更复杂的例子: https : //jsfiddle.net/x78uuwp2/
Here I created simple compile directive that takes string compiles it and puts as element body with current scope. 在这里,我创建了一个简单的编译指令,它接受字符串编译并将当前范围作为元素体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.