“在我的代码中使用angularjs插入{{”时出错

时间:2015-12-13 06:25:36

标签: javascript angularjs

我每天星期日都会收到错误。星期六到星期六工作当星期天是星期天我收到错误     错误:插值时出错:{{isOpen(dealer.Day)}}     TypeError:无法读取未定义

的属性'replace'
var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);

如果您将日期和日期更改为正常工作

var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);

我在下面添加了我的代码,请帮助我。

angular.module('myApp', [])
  .controller("myCntrl", function($scope, $filter) {

    $scope.isOpen = function(dealerSchedule) {
	var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);
      //var now = new Date();
      //---if you change day and date you will not get error
      //var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
      //var now = new Date(ds);
	  
      var times = dealerSchedule[Object.keys(dealerSchedule)[now.getDay() - 1]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");
      var nowTime = new Date('1/1/1900 ' + now.toLocaleTimeString(navigator.language, {
        hour: '2-digit',
        minute: '2-digit',
        hour12: true
      }));
	  console.log(nowTime);
      var response = (times == "Leave" ? "Leave" : (nowTime >= new Date(times[0]) && nowTime <= new Date(times[1]) ? "Open Now" : "Closed Now"));
      return response;
    };

	
    $scope.dealers = [{

      S_Email_id: "aditiya@gmail.com ",
      status: "",
      Store_Name: "Adtiya Samsung Store",
      Day: {
        "monday": "09:10 AM - 06:30 PM",
        "tuesday": "09:10 AM - 12:00 PM",
        "wednesday": "09:10 AM - 06:30 PM",
        "thursday": "09:10 AM - 06:30 PM",
        "friday": "09:10 AM - 06:30 PM",
        "saturday": "10:15 AM - 04:15 PM",
        "sunday": "10:15AM - 04:15PM"
      },
    }, {

      S_Email_id: "rajs@gmail.com",
      status: "",
      Store_Name: "sri shakthi mobile service",
      Day: {
        "monday": "09:00 AM - 06:00 PM",
        "tuesday": "09:00 AM - 06:00 PM",
        "wednesday": "09:00 AM - 06:00 PM",
        "thursday": "09:00 AM - 06:00 PM",
        "friday": "09:00 AM - 06:00 PM",
        "saturday": "09:00AM - 06:00PM",
        "sunday": "Leave"
      },

    }, {

      S_Email_id: "sprtive23@gmail.com",
      status: "",
      Store_Name: "sun mobile service center ",
      Day: {
        "monday": "08:30 AM - 07:30 PM",
        "tuesday": "02:30 PM - 07:30 PM",
        "wednesday": "08:30 AM - 07:30 PM",
        "thursday": "08:30 AM - 07:30 PM",
        "friday": "08:30 AM - 07:30 PM",
        "saturday": "08:15 AM - 02:15 PM",
        "sunday": "8:15	AM - 12:15AM"
      },

    }, {

      S_Email_id: "super@gmail.com ",
      status: "",
      Store_Name: "ragu mobile service center ",
      Day: {
        "monday": "10:00 AM - 10:00 PM",
        "tuesday": "10:00 AM - 10:00 PM",
        "wednesday": "10:00 AM - 04:00 PM",
        "thursday": "10:00 AM - 10:00 PM",
        "friday": "10:00 AM - 10:00 PM",
        "saturday": "leave",
        "sunday": "leave"
      },


    }]
    //var date = new Date();

    //$scope.hhmmsstt = $filter('date')(new Date(), 'hh:mm:ss a');
    //console.log($scope.hhmmsstt);
  })
//]]>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js'></script>
<div ng-app="myApp">
  <div ng-controller="myCntrl">
    <label>Search on Label</label>
    <br>
    <input ng-model="query" type="text" placeholder="Search for name" />
    <br>
    <br>

    <div ng-repeat="dealer in dealers">

      {{dealer.Store_Name}}
      <br>{{dealer.S_Email_id}}
      <br>{{dealer.Day}}
      <br>
      <input type="button" value="order" />
          <span>{{isOpen(dealer.Day)}}</span>
      <br>
      <br>
      <br>

    </div>

  </div>
</div>

检查我的代码我收到错误enter image description here

请不要放弃投票bcoze我不知道为什么它不工作我是技术新手。帮助我

3 个答案:

答案 0 :(得分:3)

问题在于

星期日

now.getDay() - 1将失败(getday()将返回0)

你应该检查类似的,

var dayCheck = now.getDay() == 0 ? 6 : now.getDay() - 1;
var times = dealerSchedule[Object.keys(dealerSchedule)[dayCheck]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");

答案 1 :(得分:0)

错误来自:

var times = dealerSchedule[Object.keys(dealerSchedule)[now.getDay() - 1]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");

你在这条线上想要达到的目标有点令人困惑吗?

另外,如果您要将代码放入您的问题中,请将注释掉的代码视为不需要的注释。

另外,我也考虑升级你的角度版本。

答案 2 :(得分:0)

您的代码问题在此声明中

now.getDay() - 1

Date对象中的天数是基于零的索引,因此在星期日,getDay()将返回0,上述语句将评估为-1,这是一个无效的索引在您的Day对象的键数组中。要从{0} day开始正确查找getDay(),我建议您使用getDay()返回的索引并更改您的Day对象,以便星期日首先出现与下面的规范同步。

Day: {
        "sunday": "10:15AM - 04:15PM",
        "monday": "09:10 AM - 06:30 PM",
        "tuesday": "09:10 AM - 12:00 PM",
        "wednesday": "09:10 AM - 06:30 PM",
        "thursday": "09:10 AM - 06:30 PM",
        "friday": "09:10 AM - 06:30 PM",
        "saturday": "10:15 AM - 04:15 PM"
      }

对于Day

,您的JSON对象中几乎没有任何不一致
  1. 有时您使用leave,有时使用Leave。它对于所有Day对象应该是相同的
  2. 对于第三个经销商(太阳移动服务中心),您提到星期日时间为8:15 AM - 12:15AM,而应该是'上午8:15 - 下午12:15&#39; (从早上8点15分到下午12点15分)
  3. 更正所有这些一致性,添加一些新的逻辑,用于从您的Day对象中获取日期,此处有效Pen,将显示立即打开 / 立即关闭< / strong> / 相应地保留

    希望这会有所帮助:)