重用html表单在Angularjs中添加和编辑数据

时间:2014-02-27 17:46:09

标签: javascript html angularjs

我最近一直在学习AngularJs,并且想知道是否有办法解决以下问题。

我有一个名为cardetails.html的页面,显示一些汽车详细信息,例如名称,品牌,型号等。数据从查看会话存储的服务中填充。因此,当应用程序第一次运行时,只能看到“添加”链接。单击“添加”将导航用户到名为“CarDetailsForm.html”的页面,其中包含要添加数据的表单。

提交表单后,数据将存储在会话存储中,用户将被导航回cardetails.html,这次将显示汽车详细信息以及编辑链接。现在,我需要找到一种方法来重用“CarDetailsForm.html”进行编辑。当用户单击编辑链接时,表单应与表单元素中的填充数据一起打开。我完成了大部分工作,但不确定如何正确实现“编辑”功能。我发现以下链接很有用,但我试图避免两次创建相同的表单元素。

Angular JS - Edit In Place Content Editing

另一个问题是,当用户在编辑后立即从浏览器“cardetails.html”返回“CarDetailsForm.html”时,数据仍应保留在表单上。我怎样才能做到这一点?

另外,我不确定是否需要使用自定义指令来解决此问题。任何帮助或建议将不胜感激。感谢

使用解决方案更新代码(只需使用模型绑定)

以下是cardetails.html的标记

<div ng-controller="carDetailsCtrl">
    <div>Car Details</div>
    <div ng-show="hasData">     
        <ul>
            <li>{{carDetails.name}}</li>  
            <li>{{carDetails.make}}></li>          
        </ul>
    </div>
    <div ng-hide="hasData"><a href="#carDetailsForm">add</a></div>
    <div ng-show="hasData"><a href="#carDetailsForm">edit</a></div>
</div>

CarDetailsForm.html

<form ng-submit="submit()" ng-controller="carDetailsFormCtrl">
<input type="text" id="name" required="required" ng-model="formData.name">
<input type="text" id="make" required="required" ng-model="formData.make">
<input type="submit" id="submit" value="submit" />
</form>

以下是我的脚本。

app.config([
    '$routeProvider', function ($routeProvider) {
        $routeProvider.            
            when('/carDetailsForm', { templateUrl: 'CarDetailsForm.html' }).
            //when('/carDetailsForm:carDetails', { templateUrl: 'CarDetailsForm.html' }).
            otherwise({
                templateUrl: 'cardetails.html'
            });
    }
]);


    app.factory('carDetailsService', function () {
        return {
            get: function(key) {
                return sessionStorage.getItem(key);
            },
            set: function(key, value) {
                sessionStorage.setItem(key, JSON.stringify(value));
            },
            remove: function(key) {
                sessionStorage.removeItem(key);
            },
            clearAll: function() {
                sessionStorage.clear();
            }
        };
    });

app.controller('carDetailsCtrl', ['$scope', 'carDetailsService', function ($scope, carDetailsService) {

    var carDetailsInfo = carDetailsService.get("carDetailsInfo");

    $scope.carDetails = JSON.parse(carDetailsInfo);

    $scope.hasData = false;
    if ($scope.carDetails) {
        $scope.hasData = true;
    }
}]);

app.controller('carDetailsFormCtrl', [
    '$scope', 'carDetailsService', '$location', '$routeParams', function($scope, carDetailsService, $location, $routeParams) {
        $scope.formData = JSON.parse(carDetailsService.get("carDetailsInfo")) || {} ;
        //$scope.formData = $routeParams;
        $scope.submit = function() {
            carDetailsService.set("carDetailsInfo", $scope.formData);
            $location.path('/cardetails');
        };
    }
]);

1 个答案:

答案 0 :(得分:4)

您应该拥有一条接受carDetailsInfo对象并将其绑定到表单的路由,而不是拥有两条加载相同templateUrl的唯一路由,而是将一个空对象作为默认值。然后为两个按钮操作调用相同的路径,传入对象进行编辑,但不传递给新的。

处理传递数据的示例代码:

$routeProvider.
        when('/editCarDetails:carDetailsInfo', { templateUrl: 'CarDetailsForm.html' })

app.controller('carDetailsFormCtrl', [
'$scope', 'carDetailsService', '$routeParams', '$location', 
           function($scope, carDetailsService, $routeParams, $location) {

$scope.formData = $routeParams;

来自Angular Docs