Angular - How to pull a request from another page

Asked

Viewed 312 times

0

I was studying angular and wanted to pull the information out of the main page, but I’m not able to pull the javascript from the outside.

repeat page.html:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>

<div class="container" ng-app="myApp" ng-controller="customersCtrl">
    <div class="row">
        <div class="table-responsive">
            <table class="table table-hover">
                <tbody id="myTable">
                    <tr ng-repeat="x in names | filter:test">
                        <td>{{ x }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("teste.js")
    .then(function (response) {$scope.names = response.data.records;});
});
</script>

</body>
</html>

test.js ( is to pull this information and play on the main page ) :

angular.module('myApp', []).controller('namesCtrl', function($scope) {
        $scope.names = [
            'Jani',
            'Carl',
            'Margareth',
            'Hege',
            'Joe',
            'Gustav',
            'Birgit',
            'Mary',
            'Kai'
        ];
    });
  • You want to simulate an http request or simply take the variable from the other controller?

  • I would like to take all the data that is in the test.js and show in the replay.html Because later I will need to take the variables of a database and the $http.get("test.js") will be pointed to the database, only that first I am studying how to do this by a simpler requisition to then go further

  • You then want to simulate an HTTP call so that in the future it replaces for a bank call. See below if the answer helps you.

1 answer

0


As far as I know you can’t make a call HTTP to an archive .js local ( correct me if I’m wrong )

You can use this site: MYJSON to host a file in object structure. It will generate a URL to simulate your call HTTP

I made a PLUNKER example for you to analyze.

If you want to do this locally with a .json , the process is different. I advise using the call HTTP real , because you can make the handlings error =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.