我正在尝试实现以下目标(http://jsfiddle.net/EM8hr/2/),仅作为示例
<section ng-app="Myapp">
<ul ng-controller="MainController">
<li ng-repeat="link in links">
{{link}}
</li>
</ul>
</section>
角度:
Myapp = angular.module("Myapp", [])
Myapp.controller("MainController", function($scope){
$scope.links = ["http://www.google.com", "http://www.youtube.com/watch?v=Oy1WqQbqKpA"]
});
Myapp.directive("videolink", function(){
return {
restrict: "E",
compile: function(element, attrs) {
element.text(attrs.src.replace(/www\./gi,""))
}
}
});
但无法达到预期的响应。我知道这必须与指令的链接和范围属性一起使用,但我无法弄清楚它的正确方式。有人可以帮忙吗?( http://jsfiddle.net/tRG4T/ )
Myapp.directive("videolink", function(){
return {
restrict: "E",
replace: false,
scope: {src:"@"},
link: function(scope, element, attrs) {
return attrs.src.replace(/www\./gi,"")
}
}
});