1

I am developing system using angularjs with codeigniter. What I want to do:

  • There is anchor tag [edit] for every user (user list is shown using ng-repeat) on click of edit i want to open new window.Opening new window is not an issue. I want to pass user_id to that new widow. So issue is: passing user_id.
  • When I go to new window, after edit-update done, (edit update is not an issue), I want to refresh the current application (previous widow:from where I switched).

Hope you got my issue(s).

Sample Code:

<div ng-repeat="user in allUsers">
Only displaying : {{user.user_id}}, It is OK.
<a title="Edit in new window" href='javascript:window.open(\"".base_url()."phpcontroller/loadingview/user_id \",\"User Edit\", \"width=\"+screen.width+\",height=\"+screen.height+\",fullscreen=yes,location=no\");'  >Test edit</a>
</div>
  • This HTML/php PAGE is loaded through angularjs. So it is partial, thats why I cant use php functionality(eg. base_url(), php variable) here. How can I give basepath in partial. Is there any way to declare base url globally in app.js or controllers.js, so that I can use it in partials?

Please try to give suggestions, solutions. If you not get my issues clearly, please comment. Thanks.

UPDATE : Question is not full duplicate of any question on stackoverflow.

VBMali
  • 1,360
  • 3
  • 19
  • 46
  • possible duplicate of [Open links in new window using AngularJS](http://stackoverflow.com/questions/20099784/open-links-in-new-window-using-angularjs) – Sagar Naliyapara Jun 26 '15 at 07:13
  • @SagarNaliyapara: I had given visit to mentioned link, It is not totally duplicate. Try to read the post, please. – VBMali Jun 26 '15 at 07:30
  • Are you making a hybrid application? – skubski Jun 26 '15 at 07:42
  • See, I am new to angularjs, so trying to do somethings separately, and its also the need. You can say its hybrid. At this moment this is just task, that I have to implement! – VBMali Jun 26 '15 at 07:43
  • Check this plunker and tell me if this the behaviour you're looking for? [here](http://embed.plnkr.co/6cwmkf/preview) – skubski Jun 26 '15 at 07:56
  • @skubski: Sorry, No. – VBMali Jun 26 '15 at 08:38
  • http://stackoverflow.com/questions/18420069/what-application-structure-to-use-with-angularjs-and-laravel which is probably similar what you're trying to do. You willl need to do the routing with your PHP framework and not with AngularJS. – skubski Jun 26 '15 at 08:46
  • No, not at all. Totally different! See, I have explained issue in detail. – VBMali Jun 26 '15 at 08:49
  • Your issue boils down to the structure of your application which has concequences. You want to pass data between two SPA's. – skubski Jun 26 '15 at 09:10
  • Anyway , I am ready to change the way of implementing it. But the task is : opening new window for specific stuff, do it in new window. Close the window. – VBMali Jun 26 '15 at 09:14

3 Answers3

0

if you wanted to do it in an angular way you could do something like this

angular.module('myApp', [])
    .controller('myController', ['$scope', '$window', function($scope,$window) {
  $scope.openWindow = function(greeting) {
    $window.open("http://www.google.com", "", "width=640, height=480");
  };
}]);

HTML Code

<div ng-controller = "myController"> 
<a title="Edit in new window" ng-click="openWindow() >Test edit</a>
</div>
Tik
  • 822
  • 6
  • 14
  • Nice, I think it will work for open new window, but how to pass user_id to that new window? – VBMali Jun 26 '15 at 08:40
  • You will not be able to do this with AngularJS. – skubski Jun 26 '15 at 08:47
  • @skubski: There must be some solution for this! – VBMali Jun 26 '15 at 08:52
  • You're trying to use a single page application as a multi page application. You will need to let your PHP framework handle the routing between the multiple SPA's you're building. You can retrieve the URL in JS with: window.location.href or document.URL. And you will need to parse the ID from that URL. – skubski Jun 26 '15 at 09:02
  • So problem is : how to pass that ID to URL,. – VBMali Jun 26 '15 at 09:11
  • Concatenate the id to the url and make sure the routing of your framework allows a variable in the routing. ng-href="url+{{user.id}}" target="_blank" – skubski Jun 26 '15 at 09:13
  • as skubski said angular is a framework for single page web apps, not sure exactly on what you trying to achieve, but why not use modals if you want pop ups where you can pass information through? – Tik Jun 27 '15 at 09:35
  • The openWindow() function should receive the user_id as parameter, then use it in the function to generate the URL. – Oscar Dec 18 '15 at 17:10
0

In angular js you can do something like,

 $scope.newWindow = function(value) {
    $window.open("http://your_url/routing_path/?key=value", "");
  };

where routing_path will be ur routing url and value is the part where you actually send your value.

In your script you can have like

var myApp = angular.module('myApp', []);
myApp.run(function($rootScope, $location, $http, $timeout) {

 $rootScope.$on('$routeChangeStart', function(event, next) {

     if (next.originalPath === 'your_routing_url') {
        var value = next.params.key;
     }
    });
});
Ranjith S
  • 205
  • 1
  • 5
  • Whatever value which we pass as the parameter in the url will be available in this event. It should work. – Ranjith S Jun 26 '15 at 10:23
  • There is no _$routeChangeStart_ event.on booting your app, unless there are routes defined and you trigger a default path. – skubski Jun 26 '15 at 10:40
  • @skubski: there are no two ng-apps. – VBMali Jun 26 '15 at 10:55
  • $routeChangeStart gets fired before any routing takes place. Please refer the angular js doc https://docs.angularjs.org/api/ngRoute/service/$route – Ranjith S Jun 26 '15 at 11:02
  • @SujVan: the html opened in a blank target will not be a part of your primary ng-app. – skubski Jun 26 '15 at 11:31
  • @ranjith S: It clearly says: _Broadcasted before a route change_ There won't be any route that is going to be altered... You can test this yourself in a simple plunker. You won't be able to trigger an event. – skubski Jun 26 '15 at 11:34
  • @ skubski: What i have tried is calling a redirect url from other application to an angular js application and then received the parameters which i have passed from there using '$routeChangeStart', it is working in my project.Will also check. – Ranjith S Jun 26 '15 at 11:43
  • @RanjithS: Are the people trying to read my issue or just trying to answer in general way? Because still the answerss are not going to give me any hint or solution! – VBMali Jun 26 '15 at 12:01
  • I understand now like all the scope will be lost when a new tab is opened in angular js. Please refer this for more info http://stackoverflow.com/questions/21519113/angularjs-open-a-new-browser-window-yet-still-retain-scope-and-controller-and – Ranjith S Jun 26 '15 at 12:23
  • Indeed! ;-) SujVan should rethink the structure and design of the application he is building. – skubski Jun 26 '15 at 12:39
  • @SujVan Using the data between pages is possible by using web storage. Please refer this link http://www.w3schools.com/html/html5_webstorage.asp – Ranjith S Jun 26 '15 at 12:53
  • @RanjithS: No. I don't need to use any angular js in new window. I don't care about angularjs in new window, Just doing some stuff, and come back to previous window. Is this a good practice? Should I do operations in new window with angularjs? I have done with opening new window, done with operations in new window, done with closing that window. Now what I want is: on closing of that (new) window, the previous (angularjs) window (from where I came) should be refreshed! – VBMali Jun 29 '15 at 05:28
  • Im not sure how to refresh the previous page if you are closing the other tab. But one possible way with angular js is, in new tab (assuming that is also angular js page) after doing some operations, while closing the browser tab, you can get the scope of the previous page controller and do any operations from there. – Ranjith S Jun 29 '15 at 06:29
0

Also you can use Web Storage object like,

        var myApp = angular.module('myApp', []);
    myApp.factory('anyService', function($http, $location) {

    return {
    set: function(key,value) {
        return localStorage.setItem(key,value);
    },
    get: function(key) {
        return localStorage.getItem(key);
    },
    destroy: function(key) {
        return localStorage.removeItem(key);
    }       
    };
    });

inject the service any where and get the data.

Ranjith S
  • 205
  • 1
  • 5
  • localStorage is the browser object which will be present even after the browser is closed. So use and remove it as per your requirement. – Ranjith S Jun 26 '15 at 11:17