[AngularJS] Using the Angular scope $destroy event and method
2014-11-29 06:54
447 查看
With Angular scopes, you have access to a
When you toggle ng-if, actually it triggle $scope.$destory.
Once $destory() is triggered, scope is destoried, no event and binding to the scope any more.
$destroyevent that can be used to watch $scope events. This is used for cleanup, and gives you a final opportunity to access any data on a scope. Scopes also have a (rarely used)
$destroymethod that allows you to manually destroy a scope.
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"/> <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.js"></script> <script src="app.js"></script> </head> <body ng-app="demo" ng-init="isToggled = true"> <div ng-if="isToggled"> <div ng-controller="MyCtrl as my"> Simple Form <input type="text" ng-model="my.person.firstName"/> <input type="text" ng-model="my.person.lastName"/> Welcome, {{ my.person.firstName }} {{ my.person.lastName }} <button ng-click="my.click()">Toggle</button> </div> <my-directive></my-directive> </div> <hr/> </body> </html>
angular.module("demo", []) .factory("person", function () { return { firstName: "John", lastName: "Lindquist" } }) .controller("MyCtrl", function ($scope, person) { var my = this; my.person = person; my.click = function () { $scope.$destroy(); } }) .directive("myDirective", function () { return { restrict: "E", scope: {}, template: "<div style='border: 1px solid black'>My Directive</div>", link: function (scope) { scope.$on("$destroy", function () { console.log("directive destroy"); }) } } })
When you toggle ng-if, actually it triggle $scope.$destory.
Once $destory() is triggered, scope is destoried, no event and binding to the scope any more.
相关文章推荐
- call different method with the same name, parameters and return value, select which one is called using const symbol
- [AngularJS] Using an AngularJS directive to hide the keyboard on submit
- Using MC.exe, message resources and the NT event log in your own projects
- [Angular] The Select DOM Event and Enabling Text Copy
- [AngularJS] Exploring the Angular 1.5 .component() method
- Method for browsing internet of things and apparatus using the same
- Using the Web Services and COM+ Event System in the .Net Application.
- [AngularJS] Javascript scope and AngularJS $scope
- Using IBM Rational Functional Tester: Understanding and Using the TestObject.find Method
- angularJs-The factory and service of angularJs
- [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
- Using ASP.NET 3.5's ListView and DataPager Controls: Grouping Data with the ListView Control (翻译)
- Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView (翻译)
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- Implement memory allocator and deallocator using the mechanism in SGI STL
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- Using the window.open method
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- Improve the Design and Flexibility with Extreme Programming and TDD using NUnit
- Authenticate a user against the Active Directory using the user ID and password