search
Homephp教程PHP开发A brief discussion on the detailed explanation of the scope attribute of AngularJs instructions

AngularJS uses the directive() method class to define a directive:

.directive("name",function(){
  return{
     
  };
})

The above is the main framework for defining a directive. This method accepts two parameters:

1. The first parameter: name represents the defined directive. Name (angularjs will use this name to register this command)

2. The second parameter: function. The sweet potato must return an object or a function, but usually we will return an object. What follows return is the returned object.

There is a scope attribute in the returned object, which is used to modify the scope of the instruction.

Every time when registering a directive, this directive will consider a question: Should we use my own scope, or create a child scope that inherits from the parent scope,

or create an isolated scope (Does not rely on external scope); The values ​​of the

scope attribute are false, true, and { } respectively correspond to the above scope, subscope, and isolation scope.

Let’s give three examples to fully understand the usage of these attributes.

1. scope: false

html code

<div ng-controller="myController">
  <div>
    <span>我的名字是</span><span ng-bind="name"></span><br/>
    <span>我的年龄是</span><span ng-bind="age"></span>
    <div my-directive></div>
  </div>
</div>
<script>
  angular.module("app",[])
    .controller("myController",function($scope){
      $scope.name = "kobe";
      $scope.age = 39;
    })
    .directive("myDirective",function(){
      return{
        scope:false,
        restrict:"A",
        template:"<div><h1 id="下面的部分是我们创建指令时生成的">下面的部分是我们创建指令时生成的</h1>"+
            "我的名字是:<span ng-bind=&#39;name&#39;></span><br/>"+
            "我的年龄是:<span ng-bind=&#39;age&#39;></span><br/>"+
            "输入你的新名字:<input type=&#39;text&#39; ng-model=&#39;name&#39;>"+
            "</div>"
      };
    })
</script>

Effect:

At this time, the scope of the command is the same as the scope of myController, so when we enter a new name in the input box When, the names in the upper and lower places will change accordingly, as shown in the figure below:

2. scope: true

The effect of just entering the page at this time, when we enter a new name in the input box, the same as the first one will happen The results of each experiment are different, as shown in the figure:

Here, the names in the upper part have not changed, but the names in the lower part have changed.

In this experiment, there are two points that we need to pay attention to:

1. After just entering or refreshing the page, the names of the upper and lower scopes are the same because: when the scope is true, the child scope inherits the attributes of the parent scope and method. Therefore, although name and age are not defined in the child scope, they can be inherited from myController in the parent scope. Therefore, the same name and age are shown above and below.

2. After entering a new name, the upper part remains unchanged and the lower part changes. The reason is: we are modifying the name and age on the child scope, so the lower name will change. The upper name belongs to the parent scope, and the parent scope Subscopes cannot be accessed,

so the value of the above name will not change.

3. Scope: { }

The scope part of the command is modified as follows:

scope:{
  name:"@",
  age:"@"
},

We will find that the following names and ages have no values. At this time, because the current scope is isolated, it does not know the parent role Domain properties and methods. We can assign a value to it after the instruction as follows:

<div my-directive name="aaa" age="33"></div>

For the same reason, this scope is completely isolated, so the modification is only effective for the attributes and methods of the scope of the instruction itself and has nothing to do with any other scopes , the name of myController scope will not change.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
2022年最新5款的angularjs教程从入门到精通2022年最新5款的angularjs教程从入门到精通Jun 15, 2017 pm 05:50 PM

Javascript 是一个非常有个性的语言. 无论是从代码的组织, 还是代码的编程范式, 还是面向对象理论都独具一格. 而很早就在争论的Javascript 是不是面向对象语言这个问题, 显然已有答案. 但是, 即使 Javascript 叱咤风云二十年, 如果想要看懂 jQuery, Angularjs, 甚至是 React 等流行框架, 观看《黑马云课堂JavaScript 高级框架设计视频教程》就对了。

使用PHP和AngularJS搭建一个响应式网站,提供优质的用户体验使用PHP和AngularJS搭建一个响应式网站,提供优质的用户体验Jun 27, 2023 pm 07:37 PM

在如今信息时代,网站已经成为人们获取信息和交流的重要工具。一个响应式的网站能够适应各种设备,为用户提供优质的体验,成为了现代网站开发的热点。本篇文章将介绍如何使用PHP和AngularJS搭建一个响应式网站,从而提供优质的用户体验。PHP介绍PHP是一种开源的服务器端编程语言,非常适用于Web开发。PHP具有很多优点,如易于学习、跨平台、丰富的工具库、开发效

使用PHP和AngularJS构建Web应用使用PHP和AngularJS构建Web应用May 27, 2023 pm 08:10 PM

随着互联网的不断发展,Web应用已成为企业信息化建设的重要组成部分,也是现代化工作的必要手段。为了使Web应用能够便于开发、维护和扩展,开发人员需要选择适合自己开发需求的技术框架和编程语言。PHP和AngularJS是两种非常流行的Web开发技术,它们分别是服务器端和客户端的解决方案,通过结合使用可以大大提高Web应用的开发效率和使用体验。PHP的优势PHP

如何使用PHP和AngularJS进行前端开发如何使用PHP和AngularJS进行前端开发May 11, 2023 pm 05:18 PM

随着互联网的普及和发展,前端开发已变得越来越重要。作为前端开发人员,我们需要了解并掌握各种开发工具和技术。其中,PHP和AngularJS是两种非常有用和流行的工具。在本文中,我们将介绍如何使用这两种工具进行前端开发。一、PHP介绍PHP是一种流行的开源服务器端脚本语言,它适用于Web开发,可以在Web服务器和各种操作系统上运行。PHP的优点是简单、快速、便

使用PHP和AngularJS开发一个在线文件管理平台,方便文件管理使用PHP和AngularJS开发一个在线文件管理平台,方便文件管理Jun 27, 2023 pm 01:34 PM

随着互联网的普及,越来越多的人在使用网络进行文件传输和共享。然而,由于各种原因,使用传统的FTP等方式进行文件管理无法满足现代用户的需求。因此,建立一个易用、高效、安全的在线文件管理平台已成为了一种趋势。本文介绍的在线文件管理平台,基于PHP和AngularJS,能够方便地进行文件上传、下载、编辑、删除等操作,并且提供了一系列强大的功能,例如文件共享、搜索、

如何在PHP编程中使用AngularJS?如何在PHP编程中使用AngularJS?Jun 12, 2023 am 09:40 AM

随着Web应用程序的普及,前端框架AngularJS变得越来越受欢迎。AngularJS是一个由Google开发的JavaScript框架,它可以帮助你构建具有动态Web应用程序功能的Web应用程序。另一方面,对于后端编程,PHP是非常受欢迎的编程语言。如果您正在使用PHP进行服务器端编程,那么结合AngularJS使用PHP将可以为您的网站带来更多的动态效

使用Flask和AngularJS构建单页Web应用程序使用Flask和AngularJS构建单页Web应用程序Jun 17, 2023 am 08:49 AM

随着Web技术的飞速发展,单页Web应用程序(SinglePageApplication,SPA)已经成为一种越来越流行的Web应用程序模型。相比于传统的多页Web应用程序,SPA的最大优势在于用户感受更加流畅,同时服务器端的计算压力也大幅减少。在本文中,我们将介绍如何使用Flask和AngularJS构建一个简单的SPA。Flask是一款轻量级的Py

AngularJS基础入门介绍AngularJS基础入门介绍Apr 21, 2018 am 10:37 AM

这篇文章介绍的内容是关于AngularJS基础入门介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment