2017-12-11 25 views
0

md-datepickerの値をPHPデータベースに挿入したいのですが、Dateを挿入するとデータが未定義です。 PHPページ内にmd-datepickerの値を挿入する方法Angular JS md-datepicker

reservation.html

<div id="reservation" class="reservation" ng-controller="ReservationController"> 
    <h2><i>Reservations</i></h2> 
    <div class="datepicker" ng-controller="DatePickerController"> 
    <section layout="row" layout-sm="column" layout-align="center center" layout-wrap required> 
    <md-datepicker ng-model="ctrl.myDate" md-placeholder="Enter Date" md-open-on-focus required></md-datepicker> 
    </div> 
     <input type='text' class='myBox' placeholder="Enter your name..." ng-model="username"> 
     <input type='text' class='myBox' style="margin-left:100px;" placeholder="Enter your mobile number..." ng-model="mobile_number"> 
    <div class="book"> 
    <button type="submit" class="btn btn-primary btn-lg" ng-click="insertData()">Book Your Meal</button> 
    </div> 
    </div> 

datepicker.js

app.controller('DatePickerController',['$scope',function($scope){ 
    this.myDate = new Date(); 
    var [] newDate = ctrl.MyDate.Split(T); 
    string finalDate = newdate[0]; 
    $scope.myDate = finalDate; 
    this.isOpen = false; 
}]); 

reservation.js

app.controller("ReservationController",['$scope', '$http', function ($scope,$http) { 

$scope.insertData = function(){ 
    //window.location.href='index.html#/menu'; 
    $http.post('database/push_Reservation.php',{'username':$scope.username, 'mobile_number': $scope.mobile_number, 'mydate': $scope.myDate}) 
    .then(function(data){ 
     console.log(data); 
    }); 
    } 
}]); 
+0

異なるコントローラからスコープにアクセスしようとしています。あなたはあなたのケースでそれをAngularで行うことはできません(それは階層に依存します)。 – nicolascolman

+0

@nicolascolman残り日付以外の他の要素の値が挿入されています。 – Aria

+0

htmlマークアップでmyDateのctrl.myDateを置き換えようとしましたか? – nicolascolman

答えて

0

DatepickerController:

app.controller('DatePickerController',['$scope',function($scope){ 
    this.myDate = new Date(); 
    var [] newDate = ctrl.MyDate.Split(T); 
    string finalDate = newdate[0]; 
    $scope.myDate = finalDate; 
    this.isOpen = false; 
    $scope.$emit('response', $scope.myDate); 
}]); 

ReservationController:

app.controller("ReservationController",['$scope', '$http', function ($scope,$http) { 
$scope.$on('response', function (evnt, data) { 
    $scope.myDate = data; 
    }); 
$scope.insertData = function(){ 
    //window.location.href='index.html#/menu'; 
    $http.post('database/push_Reservation.php',{'username':$scope.username, 'mobile_number': $scope.mobile_number, 'mydate': $scope.myDate}) 
    .then(function(data){ 
     console.log(data); 
    }); 
    } 
}]);