javascript - Variable capture a button and pass it to a service in angular -
hello community working project angular, stuck following:
i want capture value inside of button, value comes controller, , click pass value service , service should go controller:
html
<div ng-app="myapp"> <div ng-controller="capturactrl"> <!--capturar valor en 'algunavariable' con ng-click o ng-model se me ocurre para pasarla al service captura --> codigo capturar: <button>{{capturame}}</button> </div> <br> <div ng-controller="recibectrl"> codigo capturado: {{codigo}} </div>
js
var app = angular.module('myapp',[]); app.factory('captura', function(){ var codigo = algunavariable; // <== variable que reciba del controllar capturactrl return{ getcodes: function(){ return codigo; } } }); app.controller('capturactrl', ['$scope', 'captura', function($scope, captura){ $scope.capturame = '1111'; }]); app.controller('recibectrl', ['$scope', 'captura', function($scope, captura){ $scope.codigo = captura.getcodes(); }]);
here give code worked: https://jsfiddle.net/alx_lopz/wgfh3dla/5/
add setter function factory.
app.factory('captura', function(){ var codigo = "initial value"; // <== variable que reciba del controllar capturactrl return{ getcodes: function(){ return codigo; }, setcodes: function(value) { codigo = value; return value; } } });
add ng-click
button
codigo capturar: <button ng-click="setcodigo(capturame)"> {{capturame}} </button>
convert retrieval function
codigo capturado: {{ codigo() }}
put functions on scope
app.controller('capturactrl', ['$scope', 'captura', function($scope, captura){ $scope.capturame = '1111'; $scope.setcodigo = captura.setcodes; }]); app.controller('recibectrl', ['$scope', 'captura', function($scope, captura){ $scope.codigo = captura.getcodes; }]);
the demo on jsfiddle.
Comments
Post a Comment