[AngularJS] Using the Angular scope $destroy event and method

With Angular scopes, you have access to a $destroy event 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) $destroy method 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.

时间: 2024-10-13 12:17:50

[AngularJS] Using the Angular scope $destroy event and method的相关文章

[AngularJS] Lazy loading Angular modules with ocLazyLoad

With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading of Angular modules in large applications. 'use strict'; // Declare app level module which depends on filters, and services var App = angular.module('

[AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled

By default, Angular provides a lot of debug information on the DOM that's only necessary for tools like Protractor and Batarang. Angular 1.3 allows you to turn off the debug information to give your app a simple performance boost. See: https://docs.a

控制台获取AngularJS某个元素的Scope

如何在控制台获取到某个元素的Scope呢? 假设,页面元素为: <label>Name:</label><input type="text" ng-model="yourName" placeholder="Enter a name here"><h1>{{yourName}}</h1> → 选择input元素 → 在控制台输入"$0",显示如下: <input

[AngularJS] Exploring the Angular 1.5 .component() method

Angualr 1.4: .directive('counter', function counter() { return { scope: {}, restrict: 'EA', transclude: true, bindToController: { count: '=' }, controller: function () { function increment() { this.count++; } function decrement() { this.count--; } th

外部javascript 方法修改 angularjs 中$rootScope和$scope

修改 $rootScope var $body = angular.element(document.body);   // 1 var $rootScope = $body.scope().$root;         // 2 $rootScope.$apply(function () {               // 3     $rootScope.notesconfigs.outpath = path; }); 2. 修改 $scope var appElement = docum

[AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers

The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bindToController on the directive. Here is a blog about bindToController from thoughtramwhich explains in detail why bingToController comes into handy an

[AngularJS] New in Angular 1.3 - bindToController

If you want to use controllers, instead of a link function, you can use bindToController. <!DOCTYPE html> <html ng-app="app"> <head> <title>Egghead.io Tutorials</title> <style>body{padding:20px}</style>

AngularJS 中的scope($scope)

A.单/双向绑定 单向绑定:ng-bing="nickname",双向绑定:ng-model="nickname" B.指令继承控制器的作用域 index.html <!DOCTYPE html> <html ng-app="labApp"> <head> <title>TODO supply a title</title> <meta charset="UTF-8&qu

[AngularJS] 理解AngularJS Directive中的Scope

默认情况下, AngluarJS不会为在一个Controller下的Directive创建一个单独的作用域(Scope). 这个Directive中的所有关于$scope的变量都是在它的Controller中定义, 并且会被其他的在同一Controller中的Directive所共享, 例如以下代码: <script> (function(angular) { angular.module('zzIsolateScopeTest', []) .directive("directive