javascript - 无法访问我的 Controller Angular.js 1.3 中的属性

标签 javascript angularjs mongoose angularjs-ng-model

我正在构建一个简单的表单。 该表格有一个生日字段。 我可以选择一个日期并坚持下去。 但是当我重新加载页面时,出现错误

Error: [ngModel:datefmt] Expected `2015-03-06T23:00:00.000Z` to be a date

我知道如何解决它。我需要将 user.date_birthday 转换为日期。 所以我尝试了这个。

'use strict';

angular.module('TheNameApp')
 .controller('SettingsCtrl', function ($scope, User, Auth) {

$scope.user = User.get();
$scope.errors = {};

console.log($scope.user); // display the resource
console.log($scope.user.date_birthday); //undefined

$scope.changeInformations = function(form) {
  $scope.infos_submitted = true;
  if(form.$valid) {
    Auth.changeInformations({
      gender: $scope.user.gender,
      city: $scope.user.city,
      country: $scope.user.country,
      talent: $scope.user.talent,
      date_birthday: $scope.user.date_birthday,
      user_name: $scope.user.user_name,
      email: $scope.user.email })
    .then( function() {
      $scope.infos_message = 'Done.'
    })
    .catch( function(err) {
      err = err.data;
      $scope.errors = {};

      // Update validity of form fields that match the mongoose errors
      angular.forEach(err.errors, function(error, field) {
        form[field].$setValidity('mongoose', false);
        $scope.errors[field] = error.message;
      });
    });
  }
};

.html

    <div class="form-group">
      <label>Birthday</label>

      <input type="date" name="date_birthday" class="form-control" ng-model="user.date_birthday"/>
    </div>

user.date_birthday 未定义,但我可以在 $scope.user 中看到它 我下一步需要这个

$scope.user.date_birthday = new Date($scope.user.date_birthday);

为什么我看不到我的属性?我该如何解决这个问题?

最佳答案

假设您的User是一个资源,.get()是一个异步调用。使用回调:

User.get(function(user) {
    user.date_birthday = new Date(user.date_birthday);
    $scope.user = user;
});

关于javascript - 无法访问我的 Controller Angular.js 1.3 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28948841/

相关文章:

javascript - AngularJS ng-grid 过滤器 -- filterText 格式

javascript - 将一个输入的值放入 AngularJS 中的另一个输入表单中

arrays - 从 MongoDb 中的数组的数组中删除元素

node.js - 使用 Mongoose 中间件删除钩子(Hook)从数组中级联删除

javascript - Mongoose 仅​​更新已定义的文档值

javascript - 排序后从 Datatables.net 获取数据

javascript - React.Children.only 预计会收到一个带有 <Link> 组件的 React 元素子元素

javascript - 使用前瞻,如何确保至少包含 4 个字母数字字符 + 下划线

javascript - 使用 javascript/jquery 选择仅跟随某些元素的文本

javascript - 打印页面