var myModule = angular.module('myModule', []); myModule.factory('$alert', ['$window', function($window) { return { alert: function(text) { $window.alert(text); } };}]); var myController = function($scope, $alert) { $scope.message = function(msg) { console.log(msg); $alert.alert(msg); }; }; myController.$inject = ['$scope', '$alert']; myModule.controller("myController", myController);
<div ng-app="myModule" ng-controller="myController" ng-init="msg='test for alert'" > <input ng-model="msg"> <button ng-click="message(msg);">click me</button> <br/> {{msg}} </div>