詳解AngularJS之$window窗口對(duì)象
一個(gè)瀏覽器窗口對(duì)象的引用。它是一個(gè)全局對(duì)象,在window中是全局可用的,但是它導(dǎo)致一些問(wèn)題。在Angular中也經(jīng)常通過(guò)$window服務(wù)提到它,因此它可以被重寫(xiě)、刪除及測(cè)試。
驗(yàn)證代碼:
$window 等同于 window。 (function(){ angular.module('Demo', []) .controller('testCtrl',["$window",testCtrl]); function testCtrl($window) { $window === window; } }());
$window對(duì)象可以用來(lái)獲取瀏覽器窗口各項(xiàng)屬性(如窗口高度寬度、瀏覽器版本等等)。
1、問(wèn)題背景
通過(guò)$window對(duì)象打印出輸入框的內(nèi)容
2、實(shí)現(xiàn)源碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>AngularJS之$window窗口對(duì)象</title> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script> var app = angular.module("winApp",[]); app.controller("winCon",function($window,$scope){ $scope.phone = "15969569556"; $scope.showPhone = function(){ $window.alert("您輸入的手機(jī)號(hào)是:"+$scope.phone); }; }); </script> </head> <body ng-app="winApp"> <div ng-controller="winCon"> <input type="text" id="phone" maxlength="11" ng-model="phone"/> <button ng-click="showPhone();">顯示手機(jī)號(hào)</button> </div> </body> </html>
3、實(shí)現(xiàn)結(jié)果
PS:angularjs中書(shū)寫(xiě)window.resize功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="angular.js"></script> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope, $window) { $scope.default = "hello world"; var w = angular.element($window); w.bind('resize', function(){ console.log(new Date()) }) }); </script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <h1>{{default}}</h1> </div> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular實(shí)現(xiàn)預(yù)加載延遲模塊的示例
本篇文章主要介紹了Angular實(shí)現(xiàn)預(yù)加載延遲模塊的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Angular使用過(guò)濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫(xiě)轉(zhuǎn)換功能示例
這篇文章主要介紹了Angular使用過(guò)濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫(xiě)轉(zhuǎn)換功能,涉及AngularJS過(guò)濾器針對(duì)字符串轉(zhuǎn)換的簡(jiǎn)單使用技巧,需要的朋友可以參考下2018-03-03AngularJS基于MVC的復(fù)雜操作實(shí)例講解
下面小編就為大家分享一篇AngularJS基于MVC的復(fù)雜操作實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Angular2里獲?。╥nput file)上傳文件的內(nèi)容的方法
這篇文章主要介紹了Angular2里獲?。╥nput file)上傳文件的內(nèi)容的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09Angular4.0中引入laydate.js日期插件的方法教程
在AngularJs中我們會(huì)不可避免的使用第三方庫(kù),例如jquery插件庫(kù)。下面這篇文章主要給大家介紹了關(guān)于Angular4.0中引入laydate.js日期插件的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例
本篇文章主要介紹了在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03