简体   繁体   English

动态更改导航栏文本

[英]Dynamically change navbar text

I'm building an hybrid app for iOS and Android, and there is a section where I want to show the day. 我正在为iOS和Android开发一个混合应用程序,我想在其中展示一天。 If you open the app on Tuesday, it will say Tuesday and so on. 如果您在星期二打开应用程序,它将显示星期二,依此类推。 It is in spanish, so, where it says "Miércoles" it is Wednesday, but I wrote it manually, I want it to change so if today is Wednesday, it automatically changes. 它是西班牙语的,所以说“Miércoles”的是星期三,但是我手动编写了它,所以我想更改它,所以如果今天是星期三,它会自动更改。

I tried to change it using ng-bind and the following code: 我尝试使用ng-bind和以下代码更改它:

    var today = new Date();
  if(today.getDay() == 0){
    var hoy = "Domingo";
  } else if(today.getDay() == 1){
    var hoy = "Lunes";
  }else if(today.getDay() == 2){
    var hoy = "Martes";
  }else if(today.getDay() == 3){
    var hoy = "Miercoles";
  }else if(today.getDay() == 4){
    var hoy = "Jueves";
  }else if(today.getDay() == 5){
    var hoy = "Viernes";
  }else if(today.getDay() == 6){
    var hoy = "Sábado";
  }
  $scrope.variable = hoy;

It didn't work, no console errors or warnings. 它不起作用,没有控制台错误或警告。

该图显示了我的应用

You're declaring the same variable multiple times so the declarations after the first are ignored. 您多次声明相同的变量,因此将忽略第一个之后的声明。

It would look nicer to use a weekday array like so: 像这样使用一个工作日数组看起来会更好:

var now = new Date();
//var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var weekday = ["Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes","Sábado"];

var hoy = weekday[now.getDay()];
$scope.variable = hoy;

It looks you have a typo. 看来您有错字。

Change from

$scrope.variable = hoy;

to

$scope.variable = hoy;

Simply update your code, 只需更新您的代码,

var today = new Date(),
    weekday = [ 
                "Domingo",
                "Lunes",
                "Martes",
                "Miercoles",
                "Jueves",
                "Viernes",
                "Sábado"
              ];

var hoy = weekday[today.getDay()];
$scope.variable = hoy;

Demo: https://jsfiddle.net/1a148yrw/2/ 演示: https : //jsfiddle.net/1a148yrw/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM