Error 'angular is not defined' while upgrading AngularJS1 component - angularjs

I'm trying to do upgrade components written in AngularJS1 to Angular6. I'm taking the approach of having the wrappers for all the existing AngularJS1 component by extending "UpgradeComponent" placed under the folder "directive-wrappers" in my example. I have written this application in my local and it works fine for 2 components TestDirectiveWrapper and TestDirective2Wrapper.
But when I include the third component TestDirective3Wrapper, while loading the application I get the console error as
angular.min.js:127 Error: [$injector:unpr] http://errors.angularjs.org/1.7.5/$injector/unpr?p0=testDirective3DirectiveProvider%20%3C-%20testDirective3Directive
at angular.min.js:7
at angular.min.js:46
at Object.d [as get] (angular.min.js:43)
at angular.min.js:46
at Object.d [as get] (angular.min.js:43)
at Function.push../node_modules/#angular/upgrade/fesm5/static.js.UpgradeHelper.getDirective (static.js:872)
at new UpgradeHelper (static.js:869)
at TestDirective3Wrapper.UpgradeComponent (static.js:1170)
at new TestDirective3Wrapper (TestDirective3Wrapper.ts:9)
at createClass (core.js:9296) "<app-root _nghost-c0="">"
To mimic the same application online I have created the same angularjs directives and its wrappers in stackblitz, but here I'm getting a different error though.
Can anyone help me in resolving this issue and enable me to load all the 3 components?

I had a play with this, and I managed to get it to work.
There were a few things missing.
You didn't actually include the AngularJS 1.x file.
You didn't import the original AngularJS app module file, or any of the directives.
Once I did those two things, it worked for me.
Here is the relevant code:
In app.module.ts:
import '../AngularJsApp/app.module'; // added
declare var angular: any; // added
angular
.module('testApp')
.directive('appRoot', downgradeComponent({ component: AppComponent }));
In AngularJsApp/app.module.js:
'use strict';
const angular = require('angular'); // added
// Define the `testApp` module
angular.module('testApp', []);
// added these 3 lines
require('./test-directive/test-directive.js');
require('./test-directive2/test-directive2.js');
require('./test-directive3/test-directive3.js');

Related

Does not render component

I have created AngularJS simple project using stackblitz, i am unable to
add module name.
i have tried multiple times, as a new bee bit confused.
here is a link for my project error.
https://angularjs-nbhgkj.stackblitz.io
full project
https://stackblitz.com/edit/angularjs-nbhgkj
Issue is with the following line.
window.app = angular.module('app','date', requires);
change it to
window.app = angular.module('app', requires);
because angular.module expects module name and dependency array.

Import angularjs modules with webpack

We've got a rather large project written in angularjs and are moving gradually to angular 4. The plan is to first rewrite everything to typescript + angularjs components. We've set up an empty angularjs+webpack+typescript project and are converting legacy angularjs modules to typescript. This works fine, but because of the size of the project we would like to add the 'old' angularjs modules for now and execute those scripts so we have a full working project.
I haven't found a way to make this work. So basically we've got (I've omitted some details for brevity):
app.module.ts:
import * as angular from 'angular';
import { moduleName as module1 } from './app/converted.module1';
import { moduleName as module1 } from './app/converted.module2';
export const moduleName =
angular.module('application', [
module1,
module2,
'legacy_module_3',
'legacy_module_4'
]).name;
So module1 and module2 are already converted typescript modules. Module 3 and module 4 not. We don't want to convert those yet but do want to reference them. Lets say those modules reside in '/frontend/module3.js' and '/frontend/module4.js', how would I make this work (executed js code) with webpack?
Consider just biting the bullet and doing minimal conversion on everything in one go.
I just went through a similar exercise converting an existing angularjs project to use webpack. The conversion needed for each module is small enough that I just converted all of the modules. So where we had:
angular.module('somemodule').controller(function(){ ... })
I changed those to:
export default function SomeController() { ... }
and the module declaration files all now look like:
import SomeController from './some.controller'
export default angular.module('SomeModule', [])
.controller(SomeController)
.name;
Then the top level:
import SomeModule from './some/module'
angular.module('app', [ SomeModule ]);
It took a little while, but as the changes are largely mechanical I was able to just work systematically through the entire application. Some sub-folders were just using the 'app' module name, so I added a 'module.js' file in each folder with an appropriate module name, but otherwise there weren't any real changes to be made.
I also had to change all of the template urls into imports and I imported the '.scss' file into the top level of the app (though that wasn't really required).
Next step will be to convert controllers and directives to components, but that should be pretty straightforward now.
You've got to export each one of your modules and then import them in your app module. Notice the following example:
//..import your other ts modules
import module3 from './frontend/module3.js' //This have to be relative path
import module4 from './frontend/module4.js'
export const moduleName =
angular.module('application', [
module1,
module2,
module3.name ,
module4.name
]).name;
Also notice that your old js modules should export a module:
e.g. filters module.
import angular from 'angular';
import myNiceFilter from './myNiceFilter.filter';
var filters = angular.module('filters',[]);
filters.filter('myNiceFilter', myNiceFilter);
export default filters;

Can't make my JS Bin working "require" is not defined error react/redux

I'm trying to make this JS Bin example https://jsbin.com/pineyec/edit?html,console,output works but I've got the following error :
"ReferenceError: require is not defined
at eval (eval at <anonymous> (https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js:4:28099), <anonymous>:8:14) ...blablaba...
Don't know if it's directly related to React though... Anyone ?
Thanks.
Compare to https://gist.github.com/zvweiss/66517767889a7ed9895a
You are not able to use import in the browser, look to the top of that gist to see how he pulls the bits you need out of the Globals that are created.

AngularJS 1.5 error bootstrap IBM Mobilefirst

I seem to have problems combining an MFP hybrid (no cordova) application and angular 1.5. The same application with angular 1.4.9 works fine, but if I switch to angular1.5 then i get this error:
Error: [$injector:modulerr] Failed to instantiate module ng due to:
TypeError: Cannot set property 'aHrefSanitizationWhitelist' of null
at $$SanitizeUriProvider (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:17272:35)
at new <anonymous> (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/worklight/worklight.js:1033:23)
at Object.instantiate (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:4621:14)
at provider (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:4435:36)
at http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:367:32
at forEach (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:337:20)
at Object.provider (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:4425:9)
at ngModule (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:2476:16)
at Object.invoke (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:4606:19)
at runInvokeQueue (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/vendor/angular5.js:4499:35)
http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=ng&p1=TypeError%3A%…%2FHelloWorld%2Fandroid%2F1.0%2Fdefault%2Fvendor%2Fangular5.js%3A4499%3A35)
anyone a clue what it could be?
I had the exact same problem when I upgraded to angular 1.5.0.
The problem turned out to be with a custom implementation of Function.prototype.bind that we had in our code, it looks like this interfered with the one defined in angular.
Take at the second line on your error callstack
at new <anonymous> (http://localhost:10080/Hybrid/apps/services/preview/HelloWorld/android/1.0/default/worklight/worklight.js:1033:23)
I think worklight.js may have an implementation of prototype.bind which is incompatible with the one in angular (see https://code.angularjs.org/1.5.0/docs/api/ng/function/angular.bind)
Also seeing this on the MFP 8.0 cordova plugin.
As others have mentioned, this can be caused by polyfills for Function.prototype.bind. In particular, it seems to be caused by ones that don't properly handle calling the function as a constructor with new. Simple implementations may always return the bound object regardless of invocation, whereas the expectation is that the new operator prevails over the binding and the newly created object gets returned instead.
eg.
// create an object to bind to
var alt = {
message: 'I am the alternate'
};
// our function
function myFunc() {
console.log( this.message );
};
// bind our alternate object to this for myFunc
myFunc.bind( alt );
Standard Invocation Runs as Expected
myFunc(); // output 'I am the alternate'
Invocation via new not as Expected (this is the one that breaks angular 1.5)
new myFunc(); // also outputs 'I am the alternate'</jscodeblock>
The expected behavior is that new invocation will return a new object and not the bound one.
If you need a polyfill for Function.prototype.bind be sure it properly handles this scenario such as the one found on MDN.

textAngular.js with requireJS gives saveSelection error after change

Does anyone able to get textAngular.js with RequireJS(even if someone has working example I will replicate the settings)?
I am able to load editor but when applying h1 tags I get
$window.rangy.saveSelection is not a function on textAngular.js file with _savedSelection = $window.rangy.saveSelection();
Here are versions I am using.
Angular 1.4.8
textAngular 1.4.6
Rangy 1.3.1-dev
RequireJS 2.1.22
The issue is textAngular in its core, expects a variable rangy to be available globally (https://github.com/aaronroberson/textAngular/blob/master/src/main.js#L69).
So to fix this problem, we combined the rangy-core and rangy-selectionsaverestore into our window.rangy at the beginning of the bootstrap of our application before we initialise the app with angular.module('appname', [modules...])
define(["angular", "ngRoute", "jquery", "rangy-core", "rangy-selectionsaverestore"], function(angular, ngRoute, $j, rangyCore, rangySelectionSavereStore) {
window.rangy = $j.extend(rangyCore, rangySelectionSavereStore);
});
Configuration for requirejs to load these two modules,
'rangy-core': '../bower_components/rangy/rangy-core',
'rangy-selectionsaverestore': '../bower_components/rangy/rangy-selectionsaverestore'

Resources