angular嵌入注入服务实例
<!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/angularjs.js"></script> <style type="text/css"> *{ margin:0px; padding:0px; } body{ font-size: 32px; } .show{ background: #eeeeee; padding: 8px; width: 500px; margin:8px 0px; } </style> </head> <body> <div ng-controller="sub"> <button ng-click="ask(false,'你输入的内容不正确!')">点击</button> <button ng-click="ask(true,'你真的要删除这条记录吗')">点击</button> </div> </body> <script> var app=angular.module('myapp', []); app.factory('$confirm', function ($window) { return function(msg){ $window.confirm(msg); }; }) app.service('$student',function($window,$confirm){ return function(t,msg){ return (t) ? $confirm(msg):$window.alert(msg); } }) app.controller('sub',function($scope,$student){ $scope.ask=function(t,msg){ $student(t,msg); } }) </script> </html>