angularjs - How to get input values outside the custom directive -
i need input values outside directive(num1,num2) count result in it(addition) on click button. proper way it? here html
<div ng-app="sumapp" > <div ng-controller='sumappctrl' > <input ng-model='num1' type='text' > <span>+</span> <input ng-model='num2' type='text'> <span>=</span> <bp-sum></bp-sum> <input type="button" value="sum" ng-click="sumnumbers(num1,num2)" /> </div> </div>
and js code:
var sumapp = angular.module('sumapp', []); sumapp.controller("sumappctrl", function ($scope) { }); sumapp.directive('bpsum',function( ){ return { restrict:'e', controller: sumnumbers = function(num1,num2){ var items=[parseint(num1),parseint(num2)]; $scope.addition = _.reduce(items, function(memo, num){ return memo + num; }, 0); }, template:"<input type='text' ng-model='addition' />" } })
according me..you should develop component addition this
<div ng-app="sumapp" > <div ng-controller='sumappctrl' > <bp-sum></bp-sum> </div> </div>
directive
sumapp.directive('bpsum', function() { return { restrict: 'e', controller: function($scope) { $scope.sumnumbers = function(num1, num2) { var items = [parseint(num1), parseint(num2)]; $scope.addition = _.reduce(items, function(memo, num) { return memo + num; }, 0); }; }, template: " <input name='num1' ng-model='num1' type='text' >" + "<span>+</span>" + "<input name='num2' ng-model='num2' type='text'>" + "<span>=</span><input type='button' value='sum' ng-click='sumnumbers(num1,num2)'' />"+ "<label>sum : </label> <input type='text' ng-model='addition' />" }; });
Comments
Post a Comment