1

Iam trying to share the data returned by $http to another controller.

 $scope.getClickData=function(dataId){
            var postData={"containerInstanceId" : "3842a251-1708-4df0-b941-db27a03d91ab","fetchMetadata":true,"parameters":{"#{organizationTypeID}":"abf1c91f-fea8-4033-a731-136c339525c7"}};

            $http.post('http://latest.tapplent.info/api/app/layouts/v1/container-instance/data',postData).
                success(function(data, status, headers, config) {
                $scope.data=data;
console.log(data.containerData[0].propertyData.NULL[0].organizationTypeName.value);

                }).
                error(function(data, status, headers, config) {
                    console.log("error");
                });

        }
    });

app.controller('webView', ['$scope', function($scope) {

            console.log($scope.data);

    }]),

How can i get data to webview controller. Please help me friends how can i solve this problem.

Thanks in advance.

2
  • use angular factory, Commented May 25, 2015 at 8:33
  • on fiiring getClickData function you want send request to server and then you want to go to webView controller that will take your data from request .. am right Commented May 25, 2015 at 9:03

3 Answers 3

3

By Three ways.

1.) You can use Angular factory/services.

myApp.factory('unicornLauncher', ["apiToken", function(apiToken) {
  return new UnicornLauncher(apiToken);
}]);

https://docs.angularjs.org/guide/providers

2.) By use of broadcast and Emit.

You can share data via $broadcast or $Emit and then catch this data in any controller via $on.

https://docs.angularjs.org/api/ng/type/$rootScope.Scope

I prefer Angular factory/services over broadcast/emit due to performance.

3.) Or you can use $rootscope. But that is least preferred as rootscope is your global scope. You should not litter your global scope.

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

Comments

1

The absolutely simplest way to do this would be to write the data to the rootScope.

$rootScope = data;

A better way would be to wrap the $http call in a service with methods to perform the call and retrieve the data and inject that into both controllers.

2 Comments

Thanks for the reply I had done with both ways i.e Using $rootScope and factory method but i can't able to get the data .I dont know where i was doing wrong.
That's really strange, both should work. It might come down to some kind of race condition, i.e. your webView controller reads the value before the async call has returned. If you add a watch to the $scope.data instead of just reading it should resolve that problem. Don't forget that you either need to do angular.copy(data, $scope.data) or add an object layer in the data variable: $scope.data.asyncdata = data.
1

Other Way That I Usually Do like Angularjs Wire up a Backend 2nd last Example

controller1.js

from controller1 just pass dataId to controller2 and controller2 will contain request code that will bring data and populate in scope.

$scope.onButtonClick = function () {
     $location.url('/controller2?dataId='+33);
    //$scope.getClickData=function(dataId){}); add this func to controller2.js
}

This Code in controller1.js will

  • pass small dataId to other controller2.js

  • also navigate you to other controller2.js

controller2.js

app.controller('controller2', function($scope, $location)
{
    var ID = $location.search();
    $scope.getClickData=function(ID.dataId){
         //REQUEST YOUR DATA
    });
});

Note : As we remove (request/angular promise) getClickData() function from controller1.js and add it to controller2.js .by doing this controller2 will be responsible for its own data controller2 don't have to depend on controller1 for data

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.