0

I got an JS-object in my index.html namend user. In this object are some user with mail, name etc.

So now I want to use this object in my app.js (AngularJS). I need it there. I tried to tranfer it like this in my app.js:

var tempArr = $scope.user;

But it does not work. Is there a possibility to transfer it from JS in my index.html to app.js?

4
  • 2
    You can't have "a JS object in HTML". It exists in JavaScript only. How is this object defined in index.html? Post some code. Commented Apr 1, 2016 at 10:19
  • May be by injecting window Commented Apr 1, 2016 at 10:20
  • 2
    Assign the object to the global variable. You can use a global variable anywhere in the app. Commented Apr 1, 2016 at 10:22
  • I just assumed it's a code in a <script> tag, but yet again, it can be something completely else. Commented Apr 1, 2016 at 10:26

2 Answers 2

2

As You asked From JS to angularjs so anything that is in js is in global window scope of browser and accessible inside angularjs scope so no problem to use a variable from js to angularjs context.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to @yashdeep's answer try below app.tempArr = $window.user;
1

In your, app.js you probably have something like

var app = angular.module(/*blah blah*/);

That same app variable should be (made) accessible from the outside i.e. in the global scope. That way you can just write

app.tempArr = $scope.user;

But, of course, this is just quick and dirty solution. What I usually do is to create one global object, like

var GLOBAL = {};

and attach everything of mine on it, like

GLOBAL.app = angular.module(/*blah blah*/);
GLOBAL.someHelperFunction = function someHelperFunction(){};

and even, if needed

GLOBAL.tempArr = $scope.user;
// even better if you just clone the user values
// not just attach it to GLOBAL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.