我的項目已經沒有jquery
依賴,所以我想給jquery
代碼轉換爲純angular js
或javascript
,這就是我需要轉換主要是想jQuery的代碼轉換爲純JavaScript或AngularJS
var container = $('.dynamic-content');
container.html('<div ng-controller="TempCtrl"> <h2>About</h2> <h3>{{total}}</h3> <p>Testing the total</p> <button ng-click="update()">Update</button> </div> <script> console.log("begin"); console.log(app.controller); app.controller("TempCtrl", function ($scope) { $scope.total = 0; console.log("inside"); $scope.update = function() { $scope.total = $scope.total + 1; }; }); console.log("end") <\/script>');
var newScope = angular.element(container).scope();
var compile = angular.element(container).injector().get('$compile');
compile($(container).contents())(newScope);
,這是我plunker Pure Angularjs
,但我得到了'html'在字符串'script'標籤 –
沿然後你需要一個指令。如果你想在別的地方重用這段HTML,你可以使用它。 – Joy
是的,我甚至做到了這一點,但angularjs代碼不工作時,這樣做....這裏是我的plunker https://jsfiddle.net/fLa9o1pe/9/ –