douzhu3367 2015-10-01 07:21
浏览 78

将指令中输入的数据传递给AngularJS中的控制器

I am trying to execute inline editing in angularJS, then update data entered in Mysql table, but i am new in angular and dont know how to handle the data entered (from directive) and passing it to controller to save it in DB, so in the codes below: the variables tid still empty after editing .. how can i store new id after inline editing to send them to database?

html:

<tbody>
                    <tr ng-repeat="item in ItemsByPage[currentPage]">
                        <td>
                            <div contentEditable="true" ng-model="tid" val='tid'>
                                {{item.id}}                                    
                            </div>

                            <button ng-click="inlineupdate()" type="button">
                                Save
                            </button>
                        </td>
 </tr>
                </tbody>

in controller:

$scope.inlineupdate = function () {
                var data = {
                    tid : $scope.val,                       
                };
               var request = $http({
                    method: "post",
                    url: "inlineupdate.php",
                    params: {
                        action: "post"
                    },
                    data: data
                });
                return( request.then( handleSuccess, handleError ) );

            };

and the directive:

myApp.directive('contenteditable', function() {
            return {
                restrict: 'E',
                scope: {
                    val : '='
                },                   
                link: function(scope, elm, ctrl) {

                    // view -> model
                    elm.bind('blur', function() {
                        scope.$apply(function() {
                            ctrl.$setViewValue(elm.html());
                        });
                    });

                    // model -> view
                    ctrl.render = function(value) {
                        elm.html(value);

                    };

                    // load init value from DOM
                    ctrl.$setViewValue(elm.html());

                    elm.bind('keydown', function(event) {
                        console.log("keydown " + event.which);
                        var esc = event.which == 27,
                            el = event.target;

                        if (esc) {
                                console.log("esc");
                                ctrl.$setViewValue(elm.html());
                                el.blur();
                                event.preventDefault();                        
                            }

                    });

                }
            };

and the inlineupdate.php file to update data in mysql:

<?php
header('Content-Type: application/json');
include 'connect.php';
$db = new database();
$db->setDb_name('training');
$db->connect();
if(isset($_POST)){
$id = $_POST['val'];
$name = 'name';
$data =   $db->inlineupdate('user',array('id'=>$id),array('name'=>$name));
 echo json_encode($data);
}
mysql_close();
?>

the console fires the notice that "Undefined index: tid in inlineupdate.php file"

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算