Passing Object from One Controller to Another AngularJS -
i need pass object 1 controller , have used this solution not working.
here code:
angular.module("customerapp", []) .controller('mainctrl', function ($scope, myservice, $http, $location) { var vm = this; vm.pinformcheck = function () { vm.count++; if (vm.pinform.$valid && vm.details.pin === vm.pin && vm.count <= 2) { location.href = "http://localhost:51701/home/mainmenu"; $scope.obj = { 'cid': 'vm.details.cid', 'name': 'vm.details.name', 'pin': 'vm.details.pin', 'bal': 'vm.details.bal', 'status': 'vm.details.cardstatus' }; console.log(vm.details.bal);//the correct balance displayed in console } else { vm.failpin = true; } }; }) .controller('checkctrl', function ($scope, myservice) { $scope.data = myservice.getobj(); }) .factory('myservice', function () { var obj = null; return { getobj: function () { return obj; }, setobj: function (value) { obj = value; } } });
here view first object passed:
<body ng-app="customerapp"> <div ng-controller="mainctrl vm"> <form name="vm.pinform"> <input type="password" ng-model="vm.pin" ng-required="true" /> <p><button ng-disabled="vm.count >=3" ng-click="vm.pinformcheck();" ng-init="vm.count=0">proceed</button></p> </form> ...
here' second view need object
<html ng-app="customerapp"> <body ng-controller="checkctrl"> <div> <h1>your balance {{data.bal}}</h1> ....
the balance vm.details.bal
first view must appear in data.bal
in second view, nothing appearing.
you can save vm.details in factory. , in checkctrl
factory. factories in angularjs implement singleton pattern. saved data kept in until app exists.
you tried next thing myservice.getobj();
didn't save service.
inject myservice
mainctrl
, save details it.
Comments
Post a Comment