我正在创建一个个人网站,我可以继续更新内容,而无需操纵HTML
.我试图通过简单地加载和更新JSON
文件来实现这一点.但是现在,我无法将JSON
数据加载到scope
变量上.
HTML
<!doctype html>
<html>
<head>
<script src="angular.min.js" type="text/javascript"></script>
<script src="maincontroller.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="mainApp" ng-controller="mainController">
<div id="content">
<div ng-repeat="content in contents">
<h2>{{content.heading}}</h2>
<p>{{content.description}}</h2>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
maincontroller.js
var myapp = angular.module('mainApp', []);
myapp.controller('mainController',function($scope,$http){
$scope.contents = null;
$http.get('mainContent.json')
.success(function(data) {
$scope.contents=data;
})
.error(function(data,status,error,config){
$scope.contents = [{heading:"Error",description:"Could not load json data"}];
});
//$scope.contents = [{heading:"Content heading", description:"The actual content"}];
//Just a placeholder. All web content will be in this format …
Run Code Online (Sandbox Code Playgroud) 我正在使用Vue CLI的VueJS.所以我的所有组件都是.vue
格式化的.
在我的一个组件中,我fields
在数据部分中调用了一个数组.
//Component.vue
data() {
return {
fields : [{"name" : "foo", "title" : "Foosteria"}, {"name" : "bar", "title" : "Barrista"}]
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个计算属性是一个子集fields
//Component.vue
computed : {
subsetOfFields () {
// Something else in component data determines this list
}
}
Run Code Online (Sandbox Code Playgroud)
我已经jasmine
像这样设置了所有单元测试,它们工作正常.
//Component.spec.js
import Vue from 'vue'
import MyComponent from 'Component.vue'
describe("Component test", function() {
var myComponentVar = new Vue(MyComponent);
var vm = myComponentVar.$mount();
beforeEach(function() {
vm = myComponentVar.$mount(); …
Run Code Online (Sandbox Code Playgroud) 当我vue init webpack my-project
按照这些说明进行操作时,我会收到一个问卷,询问我是否要包括棉绒和测试。如果我说“是”,则说明该项目已经建立并进行了测试。
但是,在我的一项Vue项目中,我一开始就没有说“是”。但是现在,我想包含单元测试。
安装Karma和Jasmine的传统方法似乎太麻烦了,我不知道需要修改哪些文件才能正确连接所有依赖项。
我是否可以运行一个CLI命令,再次询问这些问题,以便它自动为我进行单元测试?
我有一个包含2个文本框的HTML文件,一个用于值,另一个用于数量.底部的结果文本将值与数量相乘并显示结果.
目的是显示屏幕上所有文本框对的总和.为此,我有一个"添加新"按钮,不断添加额外的文本框对.
出现在HTML上的第一组文本框反映了包含属性"val"和"qty"的"数字"对象数组的大小.相同的值绑定到文本框.
但是,屏幕上仅添加第一组值.当我不断添加新文本框并输入新值时,结果的值应该相应地改变,但它根本不会改变.
HTML代码
<div ng-app="adder" ng-controller="addcontrol">
<table>
<tr>
<th>Value</th><th>Quantity</th>
</tr>
<tr ng-repeat="number in numbers">
<td><input type="text" ng-model="number.val"></td>
<td><input type="text" ng-model="number.qty"></td>
<td><input type="button" ng-click="deleteNumber($index)" value= "Delete"></td>pp',[]);
</tr>
</table>
<input type="button" ng-click="add()" value="Add new">Result : {{sum}}
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
var myapp = angular.module('adder', []);
myapp.controller('addcontrol',function($scope){
$scope.numbers = [
{val:100,
qty:200,
}
];
$scope.add = function()
{
$scope.numbers.push({val:0,qty:0});
};
$scope.deleteNumber = function(val)
{
numbers.splice(val, 1);
};
var result=0;
angular.forEach($scope.numbers, function(num){
result+=(num.val * num.qty);
});
$scope.sum = result;
Run Code Online (Sandbox Code Playgroud)
});
我在这做错了什么?
Array.splice
只要a是单个值,就可以使用它。假设我有一个包含 10 个元素的数组,我想删除元素 2,4 和 8,Array.splice(index,1)
在for
循环中使用是一个坏主意,因为每个元素的索引在每次拼接操作后都会发生变化。
如何删除特定的数组项,然后相应地重新排列数组
var array = ["Apple", "Banana", "Peach", "Pumpkin", "Tomato", "Mango", "Guava"];
remove(array, 4,5); //Is there a lodash function for this?
//desired result --> ["Apple", "Banana", "Peach", "Guava"]
Run Code Online (Sandbox Code Playgroud)