Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在$http.post之后使用$http.get重新加载控制器_Angularjs_Node.js_Http_Post_Ionic Framework - Fatal编程技术网

Angularjs 在$http.post之后使用$http.get重新加载控制器

Angularjs 在$http.post之后使用$http.get重新加载控制器,angularjs,node.js,http,post,ionic-framework,Angularjs,Node.js,Http,Post,Ionic Framework,我的IONIC应用程序有问题。 我想在$http.post之后通过$http.get重新加载我从nodeJS接收到的数据。 这是我的密码: .controller('todoTodayCtrl', function($scope, AuthService, API_ENDPOINT, $http, $state, $ionicPopup) { $http.get(API_ENDPOINT.url + '/today').then(function(result) { $scop

我的IONIC应用程序有问题。 我想在$http.post之后通过$http.get重新加载我从nodeJS接收到的数据。 这是我的密码:

.controller('todoTodayCtrl', 
function($scope, AuthService, API_ENDPOINT, $http, $state, $ionicPopup) {
  $http.get(API_ENDPOINT.url + '/today').then(function(result) {
      $scope.todaylist = result.data.msg;
  });
})

.controller('todoTodayNewCtrl', 
function($scope, AuthService, API_ENDPOINT, $http, $state, $ionicPopup){
    $scope.today = {
        title: ''
    };
    $scope.todoNewButton = function() {
        $http.post(API_ENDPOINT.url + '/today', $scope.today).then(function(result) {
            $state.go('menu.todoToday');
        }, function(errMsg) {
            var alertPopup = $ionicPopup.alert({
                title: 'Nelze přidat Todo',
                template: errMsg
            });
        });
    };
})
和第一页

<ion-view title="Todo Today" ng-controller="todoTodayCtrl">
<ion-content overflow-scroll="true" padding="true" class="has-header">
    <ion-list>
        <ion-item class="item-divider"  ng-repeat="today in todaylist">{{today.title}} - {{today.time}}</ion-item>
        <button class="button button-stable button-block " ui-sref="menu.todoTodayNew">Přidat todo</button>
    </ion-list>
</ion-content>

{{today.title}-{{today.time}
Přidat todo

并用表格翻页

<ion-view title="Nové todo today">
<ion-content overflow-scroll="true" padding="true" class="has-header">
    <ion-list>
        <label class="item item-input">
            <span class="input-label">Nový úkol</span>
            <input type="text" placeholder="Nová položka" ng-model="today.title">
        </label>
        <button class="button button-stable button-block " ng-click="todoNewButton()">Přidat todo today</button>
    </ion-list>
</ion-content>

11月25日
我今天要做什么

您必须在POST请求回调中的$scope.todaylist中推送新的今天值:

$http.post(API_ENDPOINT.url + '/today', $scope.today).then(function(result) {
    $scope.todaylist.push(result);
    $state.go('menu.todoToday');
}, function(errMsg) {
    var alertPopup = $ionicPopup.alert({
        title: 'Nelze přidat Todo',
        template: errMsg
    });
});
或者通过$state将结果传递给您的todoTodayCtl

$state.go('menu.todoToday', {myParam: {some: 'thing'}})

$stateProvider.state('menu.todoToday', {
                url: '/myState',
                params: {myParam: null}, ...
然后访问控制器中的参数

$stateParams.myParam //should be {some: 'thing'}

您必须在POST请求回调中的$scope.todaylist中推送新的今天值:

$http.post(API_ENDPOINT.url + '/today', $scope.today).then(function(result) {
    $scope.todaylist.push(result);
    $state.go('menu.todoToday');
}, function(errMsg) {
    var alertPopup = $ionicPopup.alert({
        title: 'Nelze přidat Todo',
        template: errMsg
    });
});
或者通过$state将结果传递给您的todoTodayCtl

$state.go('menu.todoToday', {myParam: {some: 'thing'}})

$stateProvider.state('menu.todoToday', {
                url: '/myState',
                params: {myParam: null}, ...
然后访问控制器中的参数

$stateParams.myParam //should be {some: 'thing'}
还有我的服务器NodeJs

    apiRoutes.post('/today', passport.authenticate('jwt', {session: false}), function(req, res) {
  var token = getToken(req.headers);
  if (token) {
    var decoded = jwt.decode(token, config.secret);
    User.findOne({
      name: decoded.name
    }, function(err, user) {
      if (err) throw err;

      if (!user) {
        return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
      } else {
        console.log(req.body.title);
        var newToday = new Today({
          title: req.body.title,
          time: 'Timeee',
          user: user.name
        });
        newToday.save(function(err) {
          if (err) {
            res.json({succes: false, msg: 'Error'});
          } else {
            res.status(200).json({succes: true, msg: newToday});
          }
        });
      }
    });
  } else {
    return res.status(403).send({success: false, msg: 'No token provided.'});
  }
});
还有我的服务器NodeJs

    apiRoutes.post('/today', passport.authenticate('jwt', {session: false}), function(req, res) {
  var token = getToken(req.headers);
  if (token) {
    var decoded = jwt.decode(token, config.secret);
    User.findOne({
      name: decoded.name
    }, function(err, user) {
      if (err) throw err;

      if (!user) {
        return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
      } else {
        console.log(req.body.title);
        var newToday = new Today({
          title: req.body.title,
          time: 'Timeee',
          user: user.name
        });
        newToday.save(function(err) {
          if (err) {
            res.json({succes: false, msg: 'Error'});
          } else {
            res.status(200).json({succes: true, msg: newToday});
          }
        });
      }
    });
  } else {
    return res.status(403).send({success: false, msg: 'No token provided.'});
  }
});
解决

解决


.controller('todoTodayNewCtrl',函数($scope,AuthService,API_端点,$http,$state,$ionicPopup){$scope.today={title:'};$scope.todoNewButton=function(){$http.post(API_端点.url+/today',$scope.todaylist.push(result)},函数(errMsg){var alertPopup=$ionicPopup.alert({title:'Nelze přidat Todo',template:errMsg});};};})
如果您确定您的API工作正常,请检查POST请求是否记录了新数据,GET请求是否获得了今天的数组。另外,请定义$scope.todaylist=[]在todoTodayCtrl的顶部。如果您的两个控制器处于同一级别,请在TodoToDayCtr和todoTodayNewCtrl的父控制器中使用$rootScope或定义的$scope.todaylist=[]。我认为您的问题是这里有两个不同的作用域,彼此独立。
.controller('TodoDayNewCtrl',函数($scope,AuthService,API_端点,$http,$state,$ionicPopup){$scope.todoNewButton={title:'};$scope.todoNewButton=function(){$http.post(API_ENDPOINT.url+'/today',$scope.todaylist.push.result)},function(errMsg){var-alertPopup=$ionicPopup.alert({title:'Nelze přidat Todo',template:errMsg};};};};})
如果您确定您的API工作正常,请检查POST请求是否记录了新数据,GET请求是否获得了今天的数组。另外,请定义$scope.todaylist=[]在todoTodayCtrl的顶部。如果两个控制器处于同一级别,请在TodoToDayCtr和todoTodayNewCtrl的父控制器中使用$rootScope或定义的$scope.todaylist=[]。我认为您的问题是这里有两个不同的作用域,彼此独立。