- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人能告诉我为什么 ng-repeat 对我不起作用吗?如果 jsonFile 中出现 ng-repeat 问题,它会打印到表,但问题中的 ng-repeat 问题不会打印到表。 JSON 通常是从本地目录中提取的,但对于 codepen 添加了一个小的 JSON 作为变量。
注意:是的,将变量 $scope.jsonFile 更改为 $scope.problems 是有效的。那不是我的问题。我的问题是,一旦您解析用户输入并尝试将数据显示出来,它就不起作用。原始 json 和解析后的 json 上的 console.log() 看起来相同。这是我的问题。
检查源两个 $scope 都打印到控制台。
https://codepen.io/dneverson/pen/BvLyqW
main.js
let app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope, $http){
// #################### GLOBALS ####################
$scope.logTime = {};
// #################### FUNCTIONS ##################
//GET JSON FILE
$http.get('./data/Problems2.json').then(function(response) {
$scope.jsonFile = response.data;
console.log($scope.jsonFile);
});
// Shows or Hides button by Class ID
$scope.displayButtons = function(classID){
$(document).on('mouseenter', classID, function () {
$(this).find(":button").show();
}).on('mouseleave', classID, function () {
$(this).find(":button").hide();
});
};
// Hides an element by ID
$scope.hideElement = function(elementID){
document.getElementById(elementID).style.display = "none";
};
// Shows an element by ID
$scope.showElement = function(elementID){
document.getElementById(elementID).style.display = "block";
};
// Clears an element by ID
$scope.clearElement = function(elementID){
document.getElementById(elementID).innerHTML = "";
};
// Replaces element string by ID with given searchText
$scope.addToElement = function(elementID, str){
document.getElementById(elementID).innerHTML = str;
};
// Start time
$scope.startTime = function(){
$scope.logTime.start = Date.now();
}
// End time
$scope.stopTime = function(){
$scope.logTime.end = Date.now() - $scope.logTime.start;
$scope.logTime.start = 0;
}
// Print timed results in console
$scope.printTime = function(){
if($scope.problems.length){
console.log("Time: " + $scope.logTime.end +
"ms, Results: " + $scope.problems.length +
", Records: " + $scope.jsonFile.length);
};
}
//Add problem to Patients
$scope.addProblem = function(data){
var currDate = new Date();
console.log(data);
//_mel.melFunc('{MEL_ADD_PROBLEM("", ' + data.Description + ' , "", ' + currDate + ',"", "")}');
};
// Needed for searchJSON function
$scope.addSlashes = function(str){
return (str + '').replace(/[\\"'\(\)]/g, '\\$&').replace(/\u0000/g, '\\0');
}
// Searches JSON string for searchString
$scope.searchJSON = function(searchStr){
$scope.problems = [];
return new Promise((resolve, reject) => {
//console.log("jsonFile: " + typeof $scope.jsonFile + " content: " + $scope.jsonFile); //NOTE: [object Object],[object Object] WTF!!! on line 14 it is [{...},{...},{...},...]
searchStr = $scope.addSlashes(searchStr).replace(' ', '[^"]+');
let jsonStr = JSON.stringify($scope.jsonFile, null, ' '); // , null, ' '
let found = jsonStr.match(
new RegExp('{\\s+"id":\\s"\\w+",\\s+"description":\\s.*'
+ searchStr + '.*",\\s+"weight":\\s[\\d\\.]+\\s+}','gi')
);
if (found){
found.forEach((i) => {
// let tmp = JSON.stringify(i).replace(/\\n/g,'').replace(/\s\\/g,'').replace(/\\/g,'').replace(/"{\s/g,'{').replace(/\s}"/g,'}').replace(/,\s"/g,',"').replace(/:\s/g,':');
$scope.problems.push(JSON.parse(i));
});
}
resolve();
});
};
// ################## CALL FUNCTIONS ################
$scope.displayButtons(".tblRow");
$scope.addToElement("error", "Type in the search bar for results.");
document.getElementById("searchText").focus();
$scope.showElement("searchTable");
$("#searchText").on("paste keyup", function() { //"change paste keyup"
if( $("#searchText").val().length >= 1){
$scope.startTime();
$scope.clearElement("error");
$scope.showElement("searchTable");
let searchText = $("#searchText").val().toLowerCase();
$scope.searchJSON(searchText).then(console.log($scope.problems)); //Leaving promise in for future
$scope.stopTime();
$scope.printTime();
if($scope.problems.length == 0){
//$scope.hideElement("searchTable");
$scope.addToElement("error", "No results containing all your search terms were found.");
}
}else{
//$scope.hideElement("searchTable");
$scope.addToElement("error", "Type in the search bar for results.");
};
});
});
index.html
<!DOCTYPE html>
<html lang="en" dir="ltr" ng-app="MyApp">
<head>
<meta charset="utf-8">
<title>HHS - Problems</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/main.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--<script src="scripts/bundle.public.min.js" type="text/javascript"></script>-->
<!--<script src="scripts/DemoController.js" type="text/javascript"></script>-->
<script src="js/main.js"></script>
</head>
<body ng-controller="MyCtrl">
<!-- Search Bar-->
<div class="container well has-feedback search">
<input type="text" class="form-control" id="searchText" placeholder="Search" autocomplete="off"/>
<span class="glyphicon glyphicon-search form-control-feedback"></span>
</div>
<!-- Error Messages for User-->
<div class="container">
<div id="error"></div>
</div>
<!-- Search Results Table-->
<div class="container well" id="searchTable" style="display:none;">
<div class="row">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>Weight</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="tblRow" ng-repeat="problem in jsonFile | orderBy:'-Weight' track by $index" ng-class="{'color-grey': problem.Weight === 0, 'color-blue': problem.Weight <= 1 && problem.Weight > 0 , 'color-green': problem.Weight >= 1}">
<td>{{problem.ID}}</td>
<td>{{problem.Description}}</td>
<td>{{problem.Weight}}</td>
<td class="rowbtn"><button class="btn btn-success" ng-click="addProblem(problem)" style="display:none;">Add</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
main.css
h2{
text-align: center;
}
#error{
font-weight: bold;
color: red;
}
.search{
background-color: #00426c;
margin-bottom: 0px;
box-shadow: 0px 0px 50px #dadada;
}
.well{
box-shadow: 0px 0px 50px #dadada;
}
.rowbtn{
padding: 0 !important;
width: 52px;
}
.form-control-feedback{
top: 20px !important;
right: 20px !important;
}
.glyphicon-search{
color: #003152;
}
.color-red{
color: red;
}
.color-blue{
color: #31708f;
background-color: #d9edf7;
border-color: #bce8f1;
}
.color-green{
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.color-grey{
color: #989898;
background-color: #f1f1f1;
border-color: #eaeaea;
}
.color-yellow{
color: #8a6d3b;
background-color: #fcf8e3;
border-color: #faebcc;
}
Problems2.json
[
{"ID": "A0103","Description": "Typhoid pneumonia","Weight": 0.205},
{"ID": "A0104","Description": "Typhoid arthritis","Weight": 0.51},
{"ID": "A0105","Description": "Typhoid osteomyelitis","Weight": 0.51},
{"ID": "A021","Description": "Salmonella sepsis","Weight": 0.548},
{"ID": "A0222","Description": "Salmonella pneumonia","Weight": 0.205},
{"ID": "A0223","Description": "Salmonella arthritis","Weight": 0.51},
{"ID": "A0224","Description": "Salmonella osteomyelitis","Weight": 0.51},
{"ID": "A065","Description": "Amebic lung abscess","Weight": 0.205},
{"ID": "A072","Description": "Cryptosporidiosis","Weight": 0.451},
{"ID": "A202","Description": "Pneumonic plague","Weight": 0.205},
{"ID": "A207","Description": "Septicemic plague","Weight": 0.548},
{"ID": "A212","Description": "Pulmonary tularemia","Weight": 0.205},
{"ID": "A221","Description": "Pulmonary anthrax","Weight": 0.205},
{"ID": "A227","Description": "Anthrax sepsis","Weight": 0.548},
{"ID": "A267","Description": "Erysipelothrix sepsis","Weight": 0.548},
{"ID": "A310","Description": "Pulmonary mycobacterial infection","Weight": 0.451},
{"ID": "A312","Description": "Disseminated mycobacterium avium-intracellulare complex (DMAC)","Weight": 0.451},
{"ID": "A327","Description": "Listerial sepsis","Weight": 0.548},
{"ID": "A3681","Description": "Diphtheritic cardiomyopathy","Weight": 0.377},
{"ID": "A3684","Description": "Diphtheritic tubulo-interstitial nephropathy","Weight": 0},
{"ID": "A391","Description": "Waterhouse-Friderichsen syndrome","Weight": 0.251},
{"ID": "A392","Description": "Acute meningococcemia","Weight": 0.548},
{"ID": "A393","Description": "Chronic meningococcemia","Weight": 0.548},
{"ID": "A394","Description": "Meningococcemia, unspecified","Weight": 0.548},
{"ID": "A3983","Description": "Meningococcal arthritis","Weight": 0.51},
{"ID": "A3984","Description": "Postmeningococcal arthritis","Weight": 0.51},
{"ID": "A400","Description": "Sepsis due to streptococcus, group A","Weight": 0.548},
{"ID": "A401","Description": "Sepsis due to streptococcus, group B","Weight": 0.548},
{"ID": "A403","Description": "Sepsis due to Streptococcus pneumoniae","Weight": 0.548},
{"ID": "A408","Description": "Other streptococcal sepsis","Weight": 0.548},
{"ID": "A409","Description": "Streptococcal sepsis, unspecified","Weight": 0.548},
{"ID": "A4101","Description": "Sepsis due to Methicillin susceptible Staphylococcus aureus","Weight": 0.548},
{"ID": "A4102","Description": "Sepsis due to Methicillin resistant Staphylococcus aureus","Weight": 0.548},
{"ID": "A411","Description": "Sepsis due to other specified staphylococcus","Weight": 0.548},
{"ID": "A412","Description": "Sepsis due to unspecified staphylococcus","Weight": 0.548},
{"ID": "A413","Description": "Sepsis due to Hemophilus influenzae","Weight": 0.548},
{"ID": "A414","Description": "Sepsis due to anaerobes","Weight": 0.548},
{"ID": "A4150","Description": "Gram-negative sepsis, unspecified","Weight": 0.548},
{"ID": "A4151","Description": "Sepsis due to Escherichia coli [E. coli]","Weight": 0.548},
{"ID": "A4152","Description": "Sepsis due to Pseudomonas","Weight": 0.548},
{"ID": "A4153","Description": "Sepsis due to Serratia","Weight": 0.548},
{"ID": "A4159","Description": "Other Gram-negative sepsis","Weight": 0.548},
{"ID": "A4181","Description": "Sepsis due to Enterococcus","Weight": 0.548},
{"ID": "A4189","Description": "Other specified sepsis","Weight": 0.548},
{"ID": "A419","Description": "Sepsis, unspecified organism","Weight": 0.548},
{"ID": "A420","Description": "Pulmonary actinomycosis","Weight": 0.205},
{"ID": "A427","Description": "Actinomycotic sepsis","Weight": 0.548},
{"ID": "A430","Description": "Pulmonary nocardiosis","Weight": 0.205},
{"ID": "A480","Description": "Gas gangrene","Weight": 1.449},
{"ID": "A481","Description": "Legionnaires' disease","Weight": 0.689},
{"ID": "A483","Description": "Toxic shock syndrome","Weight": 0.548},
{"ID": "A5055","Description": "Late congenital syphilitic arthropathy","Weight": 0.51},
{"ID": "A5204","Description": "Syphilitic cerebral arteritis","Weight": 0},
{"ID": "A5440","Description": "Gonococcal infection of musculoskeletal system, unspecified","Weight": 0.51},
{"ID": "A5441","Description": "Gonococcal spondylopathy","Weight": 0.51},
{"ID": "A5442","Description": "Gonococcal arthritis","Weight": 0.51},
{"ID": "A5443","Description": "Gonococcal osteomyelitis","Weight": 0.51},
{"ID": "A5449","Description": "Gonococcal infection of other musculoskeletal tissue","Weight": 0.51},
{"ID": "A5484","Description": "Gonococcal pneumonia","Weight": 0.205},
{"ID": "A5485","Description": "Gonococcal peritonitis","Weight": 0.318},
{"ID": "A5486","Description": "Gonococcal sepsis","Weight": 0.548},
{"ID": "A666","Description": "Bone and joint lesions of yaws","Weight": 0.51},
{"ID": "A6923","Description": "Arthritis due to Lyme disease","Weight": 0.51},
{"ID": "A8100","Description": "Creutzfeldt-Jakob disease, unspecified","Weight": 0},
{"ID": "A8101","Description": "Variant Creutzfeldt-Jakob disease","Weight": 0},
{"ID": "A8109","Description": "Other Creutzfeldt-Jakob disease","Weight": 0},
{"ID": "A811","Description": "Subacute sclerosing panencephalitis","Weight": 0},
{"ID": "A812","Description": "Progressive multifocal leukoencephalopathy","Weight": 0},
{"ID": "A8181","Description": "Kuru","Weight": 0},
{"ID": "A8182","Description": "Gerstmann-Straussler-Scheinker syndrome","Weight": 0},
{"ID": "A8183","Description": "Fatal familial insomnia","Weight": 0},
{"ID": "A8189","Description": "Other atypical virus infections of central nervous system","Weight": 0},
{"ID": "A819","Description": "Atypical virus infection of central nervous system, unspecified","Weight": 0},
{"ID": "A985","Description": "Hemorrhagic fever with renal syndrome","Weight": 0},
{"ID": "B007","Description": "Disseminated herpesviral disease","Weight": 0.548},
{"ID": "B0082","Description": "Herpes simplex myelitis","Weight": 0.522},
{"ID": "B0112","Description": "Varicella myelitis","Weight": 0.522},
{"ID": "B0221","Description": "Postherpetic geniculate ganglionitis","Weight": 0},
{"ID": "B0222","Description": "Postherpetic trigeminal neuralgia","Weight": 0},
{"ID": "B0223","Description": "Postherpetic polyneuropathy","Weight": 0},
{"ID": "B0224","Description": "Postherpetic myelitis","Weight": 0.522},
{"ID": "B0229","Description": "Other postherpetic nervous system involvement","Weight": 0},
{"ID": "B0682","Description": "Rubella arthritis","Weight": 0.51},
{"ID": "B180","Description": "Chronic viral hepatitis B with delta-agent","Weight": 0.257},
{"ID": "B181","Description": "Chronic viral hepatitis B without delta-agent","Weight": 0.257},
{"ID": "B182","Description": "Chronic viral hepatitis C","Weight": 0.257},
{"ID": "B188","Description": "Other chronic viral hepatitis","Weight": 0.257},
{"ID": "B189","Description": "Chronic viral hepatitis, unspecified","Weight": 0.257},
{"ID": "B20","Description": "Human immunodeficiency virus [HIV] disease","Weight": 0.482},
{"ID": "B250","Description": "Cytomegaloviral pneumonitis","Weight": 0.451},
{"ID": "B251","Description": "Cytomegaloviral hepatitis","Weight": 0.451},
{"ID": "B252","Description": "Cytomegaloviral pancreatitis","Weight": 0.451},
{"ID": "B258","Description": "Other cytomegaloviral diseases","Weight": 0.451},
{"ID": "B259","Description": "Cytomegaloviral disease, unspecified","Weight": 0.451},
{"ID": "B2685","Description": "Mumps arthritis","Weight": 0.51},
{"ID": "B3324","Description": "Viral cardiomyopathy","Weight": 0.377},
{"ID": "B371","Description": "Pulmonary candidiasis","Weight": 0.451},
{"ID": "B377","Description": "Candidal sepsis","Weight": 0.548},
{"ID": "B377","Description": "Candidal sepsis","Weight": 0.451},
{"ID": "B3781","Description": "Candidal esophagitis","Weight": 0.451},
{"ID": "B380","Description": "Acute pulmonary coccidioidomycosis","Weight": 0.205},
{"ID": "B381","Description": "Chronic pulmonary coccidioidomycosis","Weight": 0.205},
{"ID": "B382","Description": "Pulmonary coccidioidomycosis, unspecified","Weight": 0.205},
{"ID": "B390","Description": "Acute pulmonary histoplasmosis capsulati","Weight": 0.205},
{"ID": "B391","Description": "Chronic pulmonary histoplasmosis capsulati","Weight": 0.205},
{"ID": "B392","Description": "Pulmonary histoplasmosis capsulati, unspecified","Weight": 0.205},
{"ID": "B400","Description": "Acute pulmonary blastomycosis","Weight": 0.205},
{"ID": "B401","Description": "Chronic pulmonary blastomycosis","Weight": 0.205},
{"ID": "B402","Description": "Pulmonary blastomycosis, unspecified","Weight": 0.205},
{"ID": "B410","Description": "Pulmonary paracoccidioidomycosis","Weight": 0.205},
{"ID": "B4282","Description": "Sporotrichosis arthritis","Weight": 0.51},
{"ID": "B440","Description": "Invasive pulmonary aspergillosis","Weight": 0.451},
{"ID": "B441","Description": "Other pulmonary aspergillosis","Weight": 0.451},
{"ID": "B442","Description": "Tonsillar aspergillosis","Weight": 0.451},
{"ID": "B447","Description": "Disseminated aspergillosis","Weight": 0.451},
{"ID": "B4481","Description": "Allergic bronchopulmonary aspergillosis","Weight": 0.281},
{"ID": "B4489","Description": "Other forms of aspergillosis","Weight": 0.451},
{"ID": "B449","Description": "Aspergillosis, unspecified","Weight": 0.451},
{"ID": "B450","Description": "Pulmonary cryptococcosis","Weight": 0.451},
{"ID": "C6300","Description": "Malignant neoplasm of unspecified epididymis","Weight": 0.158}
]
最佳答案
我不得不说这是一种奇怪的方法,但问题是你正在加载 $scope.problems
在 Angular 的消化周期之外。因此,Angular 不知道通知 View 进行更新,因此您永远看不到搜索结果,即使您可以 console.log($scope.problems)
并看到它确实有一个值的集合。要解决此问题而不更改您创建的结构,您需要包装将值推送到 $scope.problems
的代码部分。在 $scope.$apply()
堵塞。在您的$scope.searchJSON()
内函数进行此修改以通知 Angular 您已修改 $scope.problems
集合,然后搜索结果将显示在您的 View 中:
if (found) {
$scope.$apply(() => {
found.forEach((i) => {
// let tmp = JSON.stringify(i).replace(/\\n/g,'').replace(/\s\\/g,'').replace(/\\/g,'').replace(/"{\s/g,'{').replace(/\s}"/g,'}').replace(/,\s"/g,',"').replace(/:\s/g,':');
$scope.problems.push(JSON.parse(i));
});
});
}
关于javascript - ng-repeat 解析 JSON 后无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53822556/
在angular 6项目中,我使用angular cli命令ng g lierary @some/libName创建了 Angular 库。在我的库中,我有一个需要@ng-bootstrap/ng-b
我在 plunker 有一个简单的例子.我在一个元素上有一个 ng-show,另一个元素有一个选择。选择应该切换显示/隐藏其他(输入)元素。最初将 select 设置为 Yes 会按预期显示其他输入元
我需要一个由其定义的属性(受监控)过滤的数据列表,我不能这样做: ng-if="project.monitored" 因为我需要计算不受监控的数量。 对于这个例子,我简化了从 getProjects(
我知道 ng-show 和 ng-hide 影响元素上设置的类,而 ng-if 控制元素是否呈现为 DOM 的一部分 是否有选择 ng-if 而不是 ng-show/ng-hide 或反之亦然的示例?
我试图理解 ng-if 和 ng-show/ng-hide 之间的区别,但它们看起来相同对我来说。 选择使用其中之一时我应该记住什么区别吗? 最佳答案 ngIf ngIf 指令根据表达式删除或重新创建
我有一个基本问题,通过查看 Angular 文档或 Google 找不到答案,因此我假设这是不可能的,但我想确定一下。 基本上我想要的是使用一个变量,例如$scope.loggedIn,如下所示: H
据我了解ng-show和ng-hide影响元素上设置的类,并且 ng-if控制元素是否呈现为 DOM 的一部分。 是否有选择 ng-if 的指南超过ng-show/ng-hide或者反之亦然? 最佳答
我的 html 中有几个 div。我正在将内容导出为 PDF,当用户单击导出按钮时会下载 PDF。我希望少数 div 的内容不导出/显示在 PDF 中,但应显示在网页上。我已将 ng-if="!isE
我有一个标记按钮,当用户点击它时,标记讨论,然后标记按钮被替换为文本“成功标记”。目前,我在单击标记按钮后无法禁用 ng-click 时遇到了问题。 ng-click 对于文本“已成功标记”仍然存在,
我在 Angular 1.4.7 中有这段代码: console.log('press') 不会在按下任何键时触发。我也尝试使用 keyup 和 keydown,但它们也不起作用。 我知道一个元素需
正如标题中提到的,我不知道如何使用 ng-bind-html 在部分内部呈现 html。 这里有一个 plunker 来说明。 http://plnkr.co/edit/YFfHsahcum7XA8G
将 ng-bootstrap 安装到我的 Angular-CLI(11.0.4) 时出现错误。 什么时候 ng add @ng-bootstrap/ng-bootstrap 还有我的日志文件 [err
我想从另一个 ng-controller 访问 ng-model,这可能吗?如何实现?在下面的代码中,我使用两个 Controller ,第一个 Controller 具有 mddl1,另一个 Con
在我的应用程序中,当我对文章进行 ng-repeat 时,用户单击“我想隐藏并显示一些元素”。为此,我使用的是 videoPlaying[$index],它对于所有具有 ng-hide 的元素都可以正
预信息 我尝试将管理员和客户分开在不同的阶段或 View 中。 这样,管理员可以在将表发布给客户之前对其进行修改。 问题在表格 -> td 中有按钮来控制暂存、释放(显示)或恢复(隐藏)? 所以基本上
这个问题在这里已经有了答案: What are the nuances of scope prototypal / prototypical inheritance in AngularJS? (3
我有以下奇怪的情况 - Controller 片段 - $scope.filterNum = {0: 'filterVal1', 1: 'filterVal2', 2: 'filterVal3', 3
AngularJS 版本:1.3.8 JSFiddle: http://jsfiddle.net/uYFE9/4/ 我一直在开发一个小的 AngularJS 应用程序,遇到了一些问题。我在页面上有一个
我有一个范围对象,其中包含具有 3 个不同图像的对象。我在 div 内显示图像,该图像重复直到范围对象的长度。我有 3 个不同的 img 标签,它们具有 ng-if 属性,并且仅一次显示 3 个图像之
在 AngularJs 中没有提供 ng-enabled 指令。是否有任何适当的理由不在框架中提供该指令,因为当您可以使用 ng- 时,我们同时拥有 ng-show 和 ng-hide隐藏来实现我们的
我是一名优秀的程序员,十分优秀!