How to display information inside a module from url?

I am very new to js in general, so please be patient with me.

I need to do the simple task indicated in the topic. I did some research and tried using ui.router, but since I am not very good at coding, something went wrong.

I want this url information displayed inside the modal dialog http://prntscr.com/ashi5e

So here is the code:

angular.module('plunker', ['ui.bootstrap']); var ModalDemoCtrl = function ($scope, $modal, $log) { $scope.open = function () { var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: ModalInstanceCtrl, resolve: { items: function () { return $scope.items; } } }); }; }; var ModalInstanceCtrl = function ($scope, $modalInstance, items) { $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; 
 <!doctype html> <html ng-app="plunker"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script> <script src="http://angular-ui.imtqy.com/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script> <script src="js/example.js"></script> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> </head> <body> <div ng-controller="ModalDemoCtrl"> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h3>Log</h3> </div> <div class="modal-body"> Content </div> <div class="modal-footer"> <button class="btn btn-warning" ng-click="cancel()">Close</button> </div> </script> <button class="btn" ng-click="open()">Log</button> </div> </body> </html> 
+5
source share
2 answers

You need to get the data (for example, using a service) and put it in $ scope.items. In your modal mode, do the same: get the elements and snap them to your area. What all

 angular.module('plunker', ['ui.bootstrap']); var ModalDemoCtrl = function ($scope, $modal, $log, $http) { /* Get data here with a service or smth */ $scope.items = ''; $scope.open = function () { $http.get("YOUR_URL") .then(function(response) { $scope.items = response.data var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: 'ModalInstanceCtrl', resolve: { items: function () { return $scope.items; } } }); }); }; }; var ModalInstanceCtrl = function ($scope, $modalInstance, items) { $scope.items = items; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; 
 <!doctype html> <html ng-app="plunker"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script> <script src="http://angular-ui.imtqy.com/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script> <script src="js/example.js"></script> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> </head> <body> <div ng-controller="ModalDemoCtrl"> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h3>Log</h3> </div> <div class="modal-body"> {{items}} </div> <div class="modal-footer"> <button class="btn btn-warning" ng-click="cancel()">Close</button> </div> </script> <button class="btn" ng-click="open()">Log</button> </div> </body> </html> 
+2
source

You have not registered modalDemoCtrl and ModalInstanceCtrl , first you need to register both controllers, for example: myApp.controller('modalDemoCtrl', ModalDemoCtrl); where myApp is an angular module, for example: var myApp = angular.module('plunker', ['ui.bootstrap']); .

you can use the service to retrieve data from the url using $http and this service in your modalInstance controller. in my example, create a dataService and a function called getData and this function is called from the modalInstance controller. for example: dataService.getData().then(.. and use the function to get the response. and save the response data in the $scope.infos variable so you can use this $scope.infos in your modal format.

 var myApp = angular.module('plunker', ['ui.bootstrap']); var ModalDemoCtrl = function ($scope, $modal, $log) { $scope.open = function () { var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', controller: ModalInstanceCtrl, resolve: { items: function () { return $scope.items; } } }); }; }; myApp.controller('modalDemoCtrl', ModalDemoCtrl); myApp.factory('dataService', function($http) { return { getData: function() { return $http.get('http://prnt.sc/ashi5e'); } }; }); var ModalInstanceCtrl = function ($scope, $modalInstance, items, dataService) { dataService.getData().then(function(response) { $scope.infos = response.data; }); $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; myApp.controller('ModalInstanceCtrl', ModalInstanceCtrl); 
0
source

Source: https://habr.com/ru/post/1247196/


All Articles