Create angular app with js bookmarklet

I am trying to make an angularjs application from a site by clicking on the JS booklet.

The problem is that the ng controller is not recognized by angular and throws this error:

  Error: Argument 'LDController' is not a function, got undefined

First I define an ng application and use angular.module with the same name, this works, I got <html ng-app="bookmarklet"> . Then I upload an external file called application.js. app.controller('LDController', ..) is defined in this file!

What am I doing wrong? here is the js bookmark code:

  function loadApp () {
   document.getElementsByTagName ('html') [0] .setAttribute ('ng-app', 'bookmarklet');

   var div = document.createElement ('div');
   div.setAttribute ('ng-controller', 'LDController');
   div.innerHTML = '';
   document.body.appendChild (div);

   var js = document.createElement ('script');
   js.innerText = "var app = angular.module ('bookmarklet', []);";
   document.body.appendChild (js);

   js = document.createElement ('script');
   js.setAttribute ('src', 'http://example.org/js/application.js');
   document.body.appendChild (js);
 }

 (function () {
   if (typeof angular === 'undefined') {
     js = document.createElement ('script');
     js.setAttribute ('src', 'https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js');
     js.onload = loadApp;
     document.body.appendChild (js);
   }
   else {
     loadApp ();
   }
 } ());
+4
source share
2 answers

I thought, and you manually initialize Angular by adding the ng-app directive. The prescribed method for this is to NOT embed ng-app and instead call angular.bootstrap on the appropriate element after installing everything else. For example:

 angular.element(document).ready(function() { angular.bootstrap(document, ['bookmarklet']); } 

More information here.

+3
source

so I designed it thanks to Ram Rajamon !

here is the solution in the form of gist: https://gist.github.com/lpsBetty/5636338

+2
source

Source: https://habr.com/ru/post/1482157/


All Articles