咱們想在一個文本框輸入一些文字,而後點擊銨鈕,alert()出來。
angularjs
<div ng-app="alertApp" ng-controller="alertController"> <div> <label>Name:</label> <input type="text" ng-model="Name" /> </div> <div> <label></label> <input type="button" value="click me" ng-click="ShowMessage()" /> </div> </div>
var alertApp = angular.module('alertApp', []); alertApp.controller('alertController', function ($scope, $window) { $scope.ShowMessage = function () { if (typeof ($scope.Name) == 'undefined' || $scope.Name == "") { $window.alert("請在Name域中填寫名字。"); return; } $window.alert("Hello " + $scope.Name); } });