Passing data from one route view to another

只谈情不闲聊 提交于 2019-12-02 03:48:36

I don't want to use $rootScope to save data or create a new services ( as I have many views that pass small bits of data so creating new jsfile for few lines code is not fun)

There is no need to create a new service for new bits of data. Simply create a value service with an object:

app.value("viewData", {});

Then simply add new properties as needed:

app.controller("viewCtrl", function(viewData) {
    viewData.newProp = "new info";
    console.log(viewData.oldProp);
});

As value services are singletons, changes to the contents of the object will survive view changes.

Since you have specified the approach that you wanted to follow, you should create the following

$stateProvider
    .state('view2', {
        url: "/view2/:param1/:param2",
        templateUrl: 'view2.html',
        controller: function ($stateParams) {
            console.log($state.params.param1+"-"+$state.params.param2);
        }
    });

and from where ever you want to call

$state.go('view2',{param1:'10', param2:'2'});

But in general it is advised that you keep these values in a service or some where stored. With the application growing you may have to use few more attributes to compute

Easiest way would be to store all the data directly into the $routeParams either through route or query string.

routeUrl: /foods/:quality/:stars

then in your controller you can access

$routeParams.quality - $routeParams.stars
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!