我是一名硬件工程师,试图创建一个内部软件工具。我原以为我可以很容易地做到这一点,但有一些未知数使我无法取得进展。
我正在尝试创建一个内部软件解决方案来管理订单。我已经定义了一个有效的JSON。
我想建立一个网页,在那里我可以创建一个新的订单,通过填写网页表格。然后,应该将数据存储为JSON文本文件。我还希望能够加载JSON文本文件,用当前值预先填充表单,进行一些更改,然后保存更改。
我在php和mysql中做了类似的事情,但是我想要使用JSON文件来对软件工具进行更改,而不必修改数据库结构。我也认为这是一个很好的学习机会。
我正在尝试使用自动生成的表单(schemaform.io),并且我已经使用了以下代码:
<!DOCTYPE html>
<html >
<head>
</head>
<body ng-app="test" ng-controller="FormController">
<form name="ngform"
sf-schema="schema"
sf-form="form"
sf-model="model"></form>
<script type="text/javascript" src="../bower_components/angular/angular.js"></script>
<script type="text/javascript" src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
<script type="text/javascript" src="../bower_components/tv4/tv4.js"></script>
<script type="text/javascript" src="../bower_components/objectpath/lib/ObjectPath.js"></script>
<script type="text/javascript" src="../bower_components/angular-schema-form/dist/schema-form.min.js"></script>
<script type="text/javascript" src="../bower_components/angular-schema-form/dist/bootstrap-decorator.min.js"></script>
<script type="text/javascript" src="../bower_components/jquery/dist/jquery.js"></script>
</script>
<script>
/*
$.getJSON("data/order.json", function(orderTemplateJson) {
console.log(orderTemplateJson); // this will show the info it in firebug console
$scope.$broadcast('schemaFormRedraw')
});
*/
var app = angular.module('test',['schemaForm']);
app.controller('FormController', function($scope,$http){
$scope.schema = {
// A long long string of text goes here
};
$scope.form = [
"*",
{
type: "submit",
title: "Save"
}
];
$scope.model = {};
})
</script>
</body>
</html>
现在,我希望从文件中加载JSON模式。我试图将代码移动到getJSON调用的回调中,但得到了以下错误消息:
未知错误:$injector:modulerr无法实例化模块测试,原因是:$injector:nomod模块“测试”不可用!您要么拼错了模块名,要么忘记加载它。如果注册一个模块,请确保将依赖项指定为第二个参数。
$.getJSON("data/order.json", function(orderTemplateJson) {
console.log(orderTemplateJson);
//Moved all the angular module code to here
});
我尝试过各种各样的事情,但问题可能是我不知道自己在做什么。任何帮助都将不胜感激。
另外,有没有人知道我如何使用包含数据的JSON文件(并符合模式)预加载表单?
谢谢。。
/马丁
发布于 2016-01-20 15:55:15
在使用角度的时候,用角度的方式做事情是很好的。因此,第一件事是,您应该使用$http来加载文件,而不是使用jQuery的$.getJSON。因此,在您的控制器中,您可以:
app.controller('FormController', function($scope,$http){
$http.get("data/order.json").then(
function success(response) {
// please note that as this is asynchronous,
// the $scope.schema will be available after response
// has been received but this should be ok
$scope.schema = angular.fromJson(response.data);
},
function error(response) { /* handle error */ });
// do other stuff
});那么角$http API参考会有帮助
还有其他的事情要考虑使用角度,但值得投入一些时间来熟悉角的方式,并从中受益相对较快。
更有帮助的是使用$resource而不是$http,因为它专门用于处理json (实际上还有REST )。
https://stackoverflow.com/questions/34904187
复制相似问题