Javascript 点击问题
我想读取一个Json对象,然后我把Json元素放在我的html(用户界面)中,问题是,只有在我点击两次按钮时,该元素才会出现在视图中。 这是控制器中的功能:Javascript 点击问题,javascript,angularjs-scope,Javascript,Angularjs Scope,我想读取一个Json对象,然后我把Json元素放在我的html(用户界面)中,问题是,只有在我点击两次按钮时,该元素才会出现在视图中。 这是控制器中的功能: $scope.openfile=function () { $scope.db.select("items", { "id": { "value": $routeParams.id, }, }).then(function(results) {
$scope.openfile=function ()
{
$scope.db.select("items", {
"id": {
"value": $routeParams.id,
},
}).then(function(results) {
var parser = new xml2js.Parser();
fs.readFile(results.rows.item(0).path, function(err, data) {
parser.parseString(data, function (err, result) {
if (err == null)
{
$scope.descriptionApp=result.widget.description[0];
}
});
});
}
html:
打开文件
据我所见,您正试图从angular的scope-fs.readFile()之外的函数更新$scope。
在这种情况下,您需要使用$scope.$apply来包装操作。这样做:
fs.readFile(results.rows.item(0).path,function(err, data) {
parser.parseString(data, function (err, result) {
if (err == null)
{$scope.$apply(function(){
$scope.descriptionApp=result.widget.description[0];
});
}
});
});
fs.readFile(results.rows.item(0).path,function(err, data) {
parser.parseString(data, function (err, result) {
if (err == null)
{$scope.$apply(function(){
$scope.descriptionApp=result.widget.description[0];
});
}
});
});