2016-12-14 133 views
0

我正在使用angularjs,我想通過簡單地標記一個ng-tag來獲取html元素。AngularJS獲取html元素

<div ng-xxxx="myDiv"></div> 

console.log($scope.myDiv); //<div ng-xxxx="myDiv"></div> 

有人可以技術我怎麼可以做?

+0

http://stackoverflow.com/questions/30883995/getting-the-html-content-of-the-url-specified-in-angularjs –

+0

http://stackoverflow.com/questions/25564721/get-the-text-of-div-using-angularjs –

回答

1

這是一個應該接近你要找的東西或者至少一個好的起點的plunkr樣本。

https://plnkr.co/edit/QVd5WJEGq7B9WWyXJBSu?p=preview

因此,這裏是一個屬性指令中,使用方法結合(&結合),這意味着它接受一個angular expressiongetHtml: '&'所以這裏的父是使用表達式來設置一個範圍變量$內容get-html="$ctrl.string = $content"。當表達式在鏈接函數中執行時,$ content由指令本身在映射中定義。 scope.getHtml({ $content: elem.html() });

.directive('getHtml', function() { 
    return { 
    scope: { 
     getHtml: '&' 
    }, 
    restrict: 'A', 
    link: function(scope, elem, attr) { 
     scope.getHtml({ $content: elem.html() }); 
    } 
    }; 
}); 
+0

實際上,我正在尋找一種非指令方法。 – user3711105

+0

指令有什麼問題? :)這基本上是角度的方法,也許你只需要jQuery? – Brian