2016-02-09 51 views
0

我想添加一個帶有過濾器的a-tag到html內容。我用ng-bind-html綁定html內容,並且a-tag包含ng-click。但我的問題是,ng-click不起作用。AngularJS ng-click不起作用從篩選器

我在HTML文件中這樣

<div ng-bind-html="text | addLink"></div> 

我的過濾器看起來像這樣

app.filter('addLink', ['$sce', function($sce) { 
    return function(text) { 
    text += ''; 
    text += '<a class="btn btn-link" ng-click="readMore()">readMore</a>' 
    text = $sce.trustAsHtml(text); 
    return text; 
    } 
}]); 

,這是我的控制器

app.controller('MainCtrl', ['$scope', '$sce', function($scope, $sce) { 

    var text = '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.</p>' + 
      '<p>Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>' 

    $scope.text = $sce.trustAsHtml(text); 
    $scope.readMoreIsCalled = 'read more was not called yet'; 

    $scope.readMore = function() { 
    console.log("READ MORE"); 
    $scope.readMoreIsCalled = 'Read more method was called'; 
    } 

}]); 

I created a Plunk

有人能幫助我嗎?

謝謝!

+0

這是一個常見的問題。發生這種情況時,你的綁定HTML,但你不編譯它(它不會通過閱讀你的HTML中的所有指令)。我見過有人用一個自定義的'compile'指令來解決這個問題。還有其他答案只是谷歌周圍。 https://www.reddit.com/r/angularjs/comments/312mbg/ngclick_on_element_inserted_using_ngbindhtml_not/ – Nix

+0

而不是使用過濾器。我會建議使用帶$編譯的指令。檢查這個例子:http://stackoverflow.com/questions/22737927/angular-ng-bind-html-filters-out-ng-click –

+0

@Nix和aayush shrestha非常感謝。我嘗試了你的解決方案,現在它正在工作。 – stefOCDP

回答

1

我可以在評論的幫助下找到解決方案。我只需要創建一個新的指令,來編譯插入的html內容。 這裏是我的代碼

的index.html

<div compile="text | addLink"></div> 

過濾

app.filter('addLink', [function() { 
    return function(text) { 
    text += ''; 
    text += '<a class="btn btn-link" ng-click="readMore()">readMore</a>' 
    return text; 
    } 
}]); 

新指令

app.directive('compile', ['$compile', function ($compile) { 
    return function(scope, element, attrs) { 
    scope.$watch(
     function(scope) { 
     // watch the 'compile' expression for changes 
     return scope.$eval(attrs.compile); 
     }, 
     function(value) { 
     // when the 'compile' expression changes 
     // assign it into the current DOM 
     element.html(value); 

     // compile the new DOM and link it to the current 
     // scope. 
     // NOTE: we only compile .childNodes so that 
     // we don't get into infinite loop compiling ourselves 
     $compile(element.contents())(scope); 
     } 
); 
}; 
}]); 

控制器

app.controller('MainCtrl', ['$scope', function($scope) { 
    var text = '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.</p>' + 
      '<p>Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>' 

    $scope.text = text; 
    $scope.readMoreIsCalled = 'read more was not called yet'; 

    $scope.readMore = function() { 
    console.log("READ MORE"); 
    $scope.readMoreIsCalled = 'Read more method was called'; 
    } 

}]); 

This is the working Plunk

但注意,現在是沒有必要再使用

$scp.trustAsHtml()