Angular JS Calculadora
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<div ng-app="prueba" ng-controller="CalcCtrl">
<calculadora resultado="resultado" mostrar="mostrar()"></calculadora>
<input type="text" ng-model="resultado"/>
</div>
JavaScript
var myApp = angular.module('prueba', []);
myApp.controller("CalcCtrl", ["$scope", function($scope){
$scope.resultado = 0;
$scope.mostrar = function(){
($scope.resultado);
}
}]);
myApp.directive("calculadora", function(){
return {
restrict: 'E',
scope: {
resultado: "=",
mostrar: "&"
},
template: '<div>' +
'<input type="text" ng-model="valor1" />' +
'<button ng-click="agregar()">+</button>' +
'<input type="text" ng-model="valor2" />' +
'</div>',
replace: true,
controller: ['$scope', function($scope){
$scope.agregar = function(){
$scope.resultado =
parseInt($scope.valor1, 10) +
parseInt($scope.valor2, 10);
setTimeout($scope.mostrar, 0);
}
}]
};
});