AngularJS 中的 $scope 是什么? [英] What is $scope in AngularJS?

查看:36
本文介绍了AngularJS 中的 $scope 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手,我无法理解 AngularJS 中的 $scope 是什么.有人可以用最简单的方式解释 $scope 在 AngularJS 中做了什么以及我们可以用它做什么.请以一种你会解释完全没有编程知识的人的方式来解释它.也有人可以用最简单的方式逐行解释下面的代码吗?

function MyController($scope) {$scope.username = '世界';$scope.sayHello = function() {$scope.greeting = '你好' + $scope.username + '!';};};

解决方案

每个控制器都有一个关联的 $scope 对象.

控制器(构造函数)函数负责设置模型属性和函数.这只能通过 $scope 来完成.无论您在视图(html 文件)中应用什么函数或模型,都可以在使用范围的控制器中访问.

只有在此 $scope 对象上定义的方法才能从 HTML/视图访问.示例 - 来自 ng-click、过滤器等

现在让我们一一举出你的例子——

1.

 function MyController($scope) {$scope.username = '世界';};

在上面的示例中,您定义了任何名为 username 的属性,其值为World".假设在 html 文件中有以下代码行 -

<h1>{{data.username}}</h1></div>

这将自动从控制器获取值并将其显示在屏幕上.值得注意的是数据".标记中是 html 页面可以将控制器称为控制器的名称.这通常在控制器中或在 html 文件的顶部定义.

2.

$scope.sayHello = function() {$scope.greeting = '你好' + $scope.username + '!';};

这是您在控制器中定义的功能,您可以通过以下代码在视图中访问 -

<h1>{{data.greeting}}</h1></div>

这里,data.greeting 会自动从 sayHello 函数中选取值,即显示的值将是Hello World".用户名中的World"与之前的Hello"连接.

我希望这能消除您的疑虑.:)

I am new to AngularJS and I can't understand what $scope is in AngularJS. Can someone please explain in the simplest way possible what does $scope do in AngularJS and what can we use it for. Please explain it in a way you would explain someone with absolutely no knowledge of programming. Also can someone explain the code below line by line in the simplest way possible?

function MyController($scope) {
    $scope.username = 'World';

    $scope.sayHello = function() {
        $scope.greeting = 'Hello ' + $scope.username + '!';
    };
};

解决方案

Every controller has an associated $scope object.

A controller (constructor) function is responsible for setting model properties and functions. This can be done only through $scope. Whatever function or model you apply in View (html file), that is accessed in controller using scope.

Only methods defined on this $scope object are accessible from the HTML/view. Example - from ng-click, filters, etc.

Now Let us take your examples one by one –

1.

 function MyController($scope) {
 $scope.username = 'World';
 };

In the above example you are defining any attribute named username with its value as "World". Suppose in the html file you have the following line of code –

<div ng-controller="MyController">
<h1>{{data.username}}</h1></div>

This will automatically pick up the value from controller and display it on screen. It is worth noting that "data." in the markup is the name of the controller that the html page can refer to the controller as. This is usually defined within the controller or at the top of the html file.

2.

$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};

This is a function you have defined in a controller which you can access in view by following code –

<div ng-controller="MyController">
<h1>{{data.greeting}}</h1></div>

Here, data.greeting will automatically pick value from sayHello function i.e. the value displayed will be "Hello World". "World" from username concatenated with "Hello" before.

I hope this clears your doubt. :)

这篇关于AngularJS 中的 $scope 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆