简体   繁体   English

更改$ scope属性并注意更改

[英]Changing $scope property and watching for changes

OK, I'm 1-day new to AngularJS, so this has to be ultra-basic (I hope not an ignorant one about how AngularJS works - I admit MVC is not really my thing, but anyway...). 好的,我刚开始是AngularJS的第一天,所以这必须是非常基础的(我希望对AngularJS的工作原理一无所知-我承认MVC并不是我真正的事情,但是无论如何...)。

My Code : 我的代码:

var PP = angular.module('PP', []);

PP.controller('documentController', function ($scope) {
    $scope.documents = [
        {
            'filename': 'Untitled-1',
            'content': 'Content-1'
        },
        {
            'filename': 'Untitled-2',
            'content': 'Content-2'
        },
        {
            'filename': 'Untitled-3',
            'content': 'Content-3'
        },
    ];
});

So, I have declared a module ( PP ), with a controller ( documentController ) and a property(?) in it ( documents ). 因此,我声明了一个模块( PP ),其中包含一个控制器( documentController )和一个属性(?)( documents )。


So, I have 2 questions : 所以,我有两个问题:

  • How (any possible way is welcome) could I programmatically have access to and change the documents array? 我如何(以任何可能的方式)可以编程方式访问和更改documents数组? (insert/remove/etc) (插入/删除/等)
  • How can I set up an event which fires up when the array has been changed, eg an element has been inserted? 我如何设置一个事件,该事件在更改数组(例如已插入元素)时触发?

Accessing the array 访问数组

In your controller, you just use: 在控制器中,您只需使用:

$scope.documents.push({filename: 'Another', content: 'Document'})

You can do that in any controller function because the array is a variable shared in the scope. 您可以在任何控制器函数中执行此操作,因为数组是作用域中共享的变量。 Since it's in the scope, you can also access it in your view like so: 由于它在范围内,因此您也可以在视图中访问它,如下所示:

<button ng-click="documents.push({filename: 'Another', content: 'Document'})">
  Add document
</button>

Usually, I'd define convenience functions for this behavior in the controller to be used in views, but in this simple example, this should do. 通常,我会在视图中使用的控制器中为此行为定义便利功能,但是在这个简单示例中,应该这样做。

Detecting changes in the array 检测阵列中的变化

To watch changes in a collection, you can use $scope.$watchCollection : 要监视集合中的更改,可以使用$scope.$watchCollection

$scope.$watchCollection('documents', function(newDocuments, oldDocuments) {
  // do something when the documents array changes
});

For a comparison between $watch and $watchCollection , see Scope $watch() vs. $watchCollection() In AngularJS . 有关$watch$watchCollection之间的比较,请参阅AngularJS中的Scope $ watch()与$ watchCollection()

Note that watches can be tricky: It's not always trivial to detect changes correctly, in my experience those functions are called way more often than they should. 请注意,手表可能很棘手:正确地检测变化并不总是那么琐碎,以我的经验,这些功能的调用频率要比其应有的频繁。

Depending on your use-case, you might get by without any watches (Angular provides many ways to react on changes without using a single watch), and you probably shouldn't use $watch in your controllers anyway. 根据您的用例,您可能不需要任何监视(Angular提供了许多无需使用单个监视就可以对更改做出反应的方法)的习惯,并且您可能始终不应该在控制器中使用$ watch

You access documents the same way you'd access any other javascript array. 访问文档的方式与访问其他JavaScript数组的方式相同。 Meaning the usual operations are available. 意味着可以进行常规操作

This is how you listen for changes: 这是您监听更改的方式:

$scope.$watchCollection('documents', function(val){
    // documents have changed!
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM