The directive angular js

angular js Custom command

Custom command return instructions that define an object:

  • restrict: restrict constraint custom instructions use form, has the value A (attributes), E (element), C (category), M (note)
  • template: This attribute specifies the angular js instruction is replaced with the template html
  • scope: scope: true indicates create a scope, the scope is inherited from the parent scope (i.e., ng-controller controls scope),
    scope: {} scope create a completely isolated, not inherit any parent scope data
    scope: { "@"} create a scope of a binding way, change the parent scope of the data will change the data sub-scope, sub-scope change data will not change the data in the parent scope
    scope: { " = "} create a two-way binding sub-scopes
    scope: {" & "} to create a binding process with the parent child scope scope
  • controller: This attribute is used for communication between the instruction in an instruction defined methods and properties to be invoked by other instructions
<!DOCTYPE html>
<html ng-app="myApp">
    <head>
        <meta charset="UTF-8">
        <title>复习directive</title>
        <script type="text/javascript" src="../js/angular.js"></script>
    </head>
    <body ng-controller="myController">
        <h4>父作用域:{{message}}</h4>
        <input type="text" ng-model="message" />
        <h4>子作用域:</h4>
        <h4>scope={}时,子作用域和父作用域完全隔离</h4>
        <div my-directive></div>
        <h4>scope={"="}的时候,实现子作用域与父作用域的双向数据绑定</h4>
        <div my-directive1 message="message"></div>
        <h4>scope={"@"}时,实现子作用域和父作用域的单向数据绑定,
            <br/>父作用域值的改变会影响子作用域,子作用域不会影响父作用域</h4>
        <div my-directive2 message={{message}}></div>
        <script>
            var app = angular.module("myApp", []);
            app.controller("myController", function($scope){
                $scope.message = "father info"
            });
            app.directive("myDirective", function(){
                return {
                    restrict: "A",
                    scope: {
                    },
                    template: "<div ng-init=\"message='child info'\">child message:{{message}}<br /><input type='text' ng-model='message'/></div>"
                };
            });
            app.directive("myDirective1", function(){
                return {
                    restrict: "A",
                    template: "<div>child message:{{message}}<br /><input type='text' ng-model='message'/></div>",
                    scope: {
                        message: "="
                    }
                }
            });
            app.directive("myDirective2", function(){
                return{
                    retrict: "A",
                    template: "<div>child message:{{message}}<br/><input type='text' ng-model='message'/></div>",
                    scope: {
                        message: "@"
                    }
                }
            });
        </script>
    </body>
</html>

Guess you like

Origin www.cnblogs.com/techi/p/11986502.html