0

I want to use a variable data in another js file. How do I do that in anularjs.

In one.js file i have a variable tagText="SAP" and I want to use its value in another js file(two.js). How do I do that in angularjs?

Can anyone help please?

  • so access a variable inside one controller from another controller? show the relevant code please. – Omar Einea Jan 02 '18 at 11:31
  • access value from one controller to a directive. I have a variable $scope.tagText in the controller (searchController.js) and I want to read this value in my directive(autoComplete.js). – Rahul Pandit Jan 02 '18 at 11:47
  • Possible duplicate of [AngularJS: How can I pass variables between controllers?](https://stackoverflow.com/questions/12008908/angularjs-how-can-i-pass-variables-between-controllers) – Omar Einea Jan 02 '18 at 12:04

1 Answers1

0

As of my point of view you can create a service of that file oneService.js

myApp.service('oneService', oneService);

function oneService(){
    var tagText;
    return {
        getTagText:getTagText,
        setTagText:setTagText
    };
    function getTagText(){
        return tagText;
    }
    function setTagText(tagText){
        this.tagText=tagText;
    }
}

Inject this service in any controller and you can access tagText variable on that controller via getter & setter

Service in Singleton in angular, at a time you have latest value of variable

Meet Patel
  • 482
  • 4
  • 12