基本上,我正在尝试创建一个远程文件浏览器。当用户单击文件夹时,该指令应使用新参数(路径)重新加载。
请参阅下面的代码。
NwkApp.directive('wip', function ($parse, $http, $rootScope, $compile) {
var WipAPI = 'example.com';
if($rootScope.path === undefined) {
$rootScope.path = new Array();
WipAPI += "?Path="+$rootScope.path.join('/');
}
return {
restrict: 'E',//This makes it relevant to the Element. Other values could be A for attr, or M for comment
replace: true,
templateUrl: WipAPI,
scope : {
data: '='//binds whatever is in data, a reference to an var, obj or array.
},
link: function (scope, element, attrs) {
scope.wipBrowse = function(subpath) {
$rootScope.path.push(subpath);
element.parent().html('<wip></wip>');
}//End wipBrowse
}//End link folder
}//End return
});
基本上,模板HTML包含多个ng-click
,它们会触发scope.wipBrowse
一次。一旦我用父html替换(希望我再次触发指令),没有任何反应。
是否有一种简单的方法可以触发指令再次运行?
答案 0 :(得分:1)
我不确定这是否会对您有所帮助,但点击事件未触发,因为您的html已被替换但未编译。我对你的指令做了一个略微修改的版本:
app.directive('wip', function($compile, $rootScope) {
return {
restrict: 'E', //This makes it relevant to the Element. Other values could be A for attr, or M for comment
replace: true,
templateUrl: 'test.html',
scope: {
data: '=' //binds whatever is in data, a reference to an var, obj or array.
},
link: function(scope, element, attrs) {
scope.wipBrowse = function(subpath) {
alert('alert');
$rootScope.path.push(subpath);
element.replaceWith($compile('<wip></wip>')(scope)[0]);
} //End wipBrowse
} //End link folder
} //End return
});