如何只將兩個屬性傳遞給$ scope變量

[英]how to pass only two properties to a $scope variable


ANGULARJS Question:

I got an array of objects and I need to pass it to my $scope variable. The property that's creating a problem for me in special is the user property since it holds elements like the name and email it affects the way the filter i set in the HTML is filtering the objects I want to display. The object is a list of notes and I want to be able to filter them by content in the note( title and body text)

我有一個對象數組,我需要將它傳遞給我的$ scope變量。特別為我創建問題的屬性是用戶屬性,因為它包含名稱和電子郵件等元素,它影響我在HTML中設置的過濾器過濾我想要顯示的對象的方式。該對象是一個筆記列表,我希望能夠按照筆記(標題和正文)中的內容過濾它們

I have tried to delete the property user from the object with the code below, but that does not work. $scope.notes still loads that attribute.

我試圖用下面的代碼從對象中刪除屬性用戶,但這不起作用。 $ scope.notes仍然加載該屬性。

Ideally I should be able to pass to $scope.notes only the title and body attributes. Any ideas of how to do that efficiently?

理想情況下,我應該只能傳遞給$ scope.notes標題和body屬性。有關如何有效地做到這一點的任何想法?

 var notes = notesService.notesObjectInService;
    for (var i = 0;  i < notes.length; i++) {
        delete notes[i].user;
    };

    $scope.notes = notes;

This is the json object passed to notes in the first line.

這是傳遞給第一行中的注釋的json對象。

[{"id":184,
"title":"Mari",
"body":"Mae",
"created_at":"2015-05-09T03:23:04.250Z",
"updated_at":"2015-05-09T03:23:04.250Z",
"user_id":1,
"user":{"id":1,
"email":"vini@vini.com",
"created_at":"2015-04-24T22:49:21.797Z",
"updated_at":"2015-05-09T03:04:27.739Z",
"username":"vinivini"}}]

1 个解决方案

#1


1  

How about adding this function to your notesService?

如何將此功能添加到notesService?

function getSummaryNotes() {
  var returnValue = [];

  for (var i = 0;  i < notes.length; i++) {
    var note = notes[i];
    returnValue.push({title: note.title, body: note.body});
  }

  return returnValue;
}

Where notes is your array of notes which presumably the service has access to.

哪些筆記是你的服務可以訪問的筆記數組。

Then you can do:

然后你可以這樣做:

$scope.notes = notesService.getSummaryNotes();

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/09/72503703a957878175c5d7ca233fc550.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com