我正在使用 Angular js 指令并绑定数据以显示在视图中。我有条件的看法。我使用相同的视图来读取数据以及编辑数据。我管理一个标志 edit=true 并在标志“edit”上使用 ng-switch 我更改了视图。
它在大多数时间都运行良好;但是我意识到即使场景背后的数据得到更新,某些视图也不会得到更新。我基本上做什么;在编辑模式下单击“确定”按钮;使用 ng-click 我更改了标志 edit=undefined 并且由于数据已更改,因此我的假设是它会更改视图。它在大多数时候确实有效,但突然停止工作。我通过打印数据检查了控制台,我看到 ng-click 实际上正在正确更新标志。以下是代码的摘录。
查看代码* [更新] *:
<!--In case of edit render it as text box-->
<ng:switch on="file.edit">
<div ng:switch-when="true">
<input type="text" ng-model="file.name" class="file-label" value="{{file.name}}" />
<i class="icon-ok" ng-click="addFile({{$index}})"></i>
<i class="icon-remove" ng-click="cancelAddFile({{$index}})"></i>
</div>
<div ng:switch-when="undefined">
<!--if it is not edit case then render as label-->
<!--Add file name as a label-->
<span class="file-label" >{{file.name}}</span>
</div>
</ng:switch>
以下是指令代码的摘录* [更新] *。
//project directive as element for project contents
app.directive("projectcontents", ['Contents', function (projectContents) {
return {
restrict: "E",
templateUrl: "/xopus/view/projectstructure/ProjectContents.html",
replace: true,
scope: {
project: '='
},
link: function (scope, element, attrs) {
/*Edit file in project*/
scope.editFile = function (fileIndex) {
//print log for debug purpose
console.log("Edit file at index " + fileIndex);
//make service call
//update edit flag
scope.files[fileIndex].edit = true;
};
/*Remove file in project*/
scope.deleteFile = function (fileIndex) {
//make service call
//print log for debug purpose
console.log("Remove file at index " + fileIndex);
//Remove this file entry form data model
scope.files.splice(fileIndex, 1);
};
/*Add file in project*/
scope.addFile = function (fileIndex) {
//print log for debug purpose
console.log("Add file at index " + fileIndex);
//make service call
//update data binding to update edit=false
//update edit flag
scope.files[fileIndex].edit = undefined;
};
/*Cancel add file */
scope.cancelAddFile = function (fileIndex) {
//print log for debug purpose
console.log("Cancel file at index " + fileIndex);
//Remove this file entry form data model
scope.files.splice(fileIndex, 1);
};
}
};
} ]);
我还在学习 Angular,我肯定会犯一些基本错误。请帮忙。