简体   繁体   English

角函数切换布尔值

[英]Angular function to switch boolean value

It seems like I'm having a pretty simple problem but can't seem to find a solution. 似乎我遇到了一个非常简单的问题,但似乎找不到解决方案。

I'm trying to convert an application from vanilla JS to the angular framework. 我正在尝试将应用程序从原始JS转换为有角度的框架。 Previously, my program had an array of objects, and clicking a button would trigger a function which would just toggle one object's boolean value between true or false. 以前,我的程序有一个对象数组,单击按钮会触发一个函数,该函数只会在一个对象的布尔值true或false之间切换。 The objects with value “true” would then be filtered out and displayed further down the page. 然后将滤除值为“ true”的对象,并在页面的下方显示。

The function was... 该功能是...

function addRepair(here, needed) {
    possibleRepairs[here].isNeeded = needed;
    var filteredArray = possibleRepairs.filter(ind => ind.isNeeded).map(ind => ind.area + " " + ind.repair).join("\<br\>");
    document.getElementById('repairsGoHere').innerHTML = filteredArray;
}

I'm having trouble recreating this in angular. 我在重新创建角度时遇到麻烦。 I think I've figured out most pieces of the process, but I don't know how to code the function that's supposed to do the actual toggle. 我想我已经解决了大部分过程,但是我不知道如何编写应该进行实际切换的函数。 Basically, I'm not sure how to access the array that I've created and change the boolean assignment. 基本上,我不确定如何访问已创建的数组以及如何更改布尔值分配。

So far in Angular I've created a class “repairs” and in my file app.component.ts I've exported a list of “repairs” in repairList like so… 到目前为止,我已经在Angular中创建了一个“修复”类,并且在我的文件app.component.ts中,我已经在repairList中导出了“修复”列表,就像这样……

repairList = [
    new repairs("Front wheel", "out of true",false),
    new repairs("Front hub", "needs tightening", false),
    ...
];

In the app.component.html I've created a button which is supposed to do the toggle… 在app.component.html中,我创建了一个按钮,该按钮应该可以进行切换…

<button type="button" onclick="addRepair('0', true);">Out of true</button>

which calls a function (which I know has been imported properly)... 哪个调用一个函数(我知道已经正确导入了)...

function addRepair(here, needed) {
    repairList[here].isNeeded = needed;
}

and later on the page will display data as so… 稍后在页面上将如此显示数据…

<div class="col-md-8" id="repairsGoHere" ng-repeat="inst in repairList | filter: {isNeeded:true}">
    <p>{{ inst.area }}</p>
</div>

any advice on how to code the function to get it to do what I'd like? 关于如何对函数进行编码以使其能够执行我想要的任何建议? Or should I be approaching this a totally different way? 还是我应该以一种完全不同的方式来对待这个问题?

Thanks for all the help! 感谢您的所有帮助!

Evan 埃文

After getting some responses on here y'all helped me figure it out - I was coding in Angular 6 and didn't realize ng-repeat was from the AngularJS-era. 在这里得到一些回应后,大家都帮助我弄清楚了-我当时在Angular 6中进行编码,却没有意识到ng-repeat来自AngularJS时代。 Ang 6 doesn't have the same filter features so I'll need to figure out how to achieve the same result as I was seeking. Ang 6没有相同的过滤器功能,因此我需要弄清楚如何获得与所寻求的结果相同的结果。 Thanks for all the input! 感谢所有的投入!

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

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