【发布时间】:2014-09-12 02:13:54
【问题描述】:
好的,这是一个特定的案例,所以我在搜索类似问题时遇到了麻烦。
我正在使用 Angular 并链接到 Firebase 后端来尝试返回真值或假值。我无法理解 Firebase 下载对象 ref 的变量的相应属性 {yesterdayAm: false, yesterdayPm: false, todayAm: false, todayPm: false} 下的一组 4 个真值或假值。当我简单地运行它时,我得到了正确且可预测的回调,我真的很愚蠢,因为我不理解 if 或 switch 表达式 Angular/Javascript 方面。帮我返回正确的值?
我将从视图开始:
<div class="col-xs-6 ">
<div class="dexbowl"><img ng-src="{{ isFed('yesterdayAm') }}"/></div>
</div>
<div class="col-xs-6">
<div class="dexbowl"><img ng-src="{{ isFed('yesterdayPm') }}"/></div>
</div>
<div class="col-xs-5 col-xs-offset-1">
<div class="dexbowl"><img ng-click="feed('todayAm')" ng-src="{{ isFed('todayAm') }}"/></div>
</div>
<div class="col-xs-5">
<div class="dexbowl"><img ng-click="feed('todayPm')" ng-src="{{ isFed('todayPm') }}"/></div>
</div>
我正在尝试通过单击按钮来喂狗。从img 的ng-src 调用的4 个isFed('[dayAm/Pm]') 函数。这些将返回各自状态的图像文件。 $scope中的状态如下:
var blueFilled = 'images/dexterbluefilled.svg';
var satBlueFilled = 'images/SATdexterbluefilled.svg'; /* not used yet */
var blueEmpty = 'images/dexterblueempty.svg';
var satBlueEmpty = 'images/SATdexterblueempty.svg'; /*not used yet */
它们通过$scope中的这个函数返回:
if (Fed.getInitState(when)) {
return blueFilled;
} else {
return blueEmpty;
};
您可能会问,传递todayAm/todayPm/yesterdayAm/yesterdayPm 参数的Fed.getInitState(when) 函数是什么。你是对的。 Fed 是从 Firebase 加载的 service(或工厂),如下所示:
getInitState: function(when) {
var value = ref[when];
return value;
/* value to equal "true" or "false", depending on server state of todayAm, todayPm, yesterdayAm, yesterdayPm */
},
同样,ref 对象是:
{yesterdayAm: false, yesterdayPm: false, todayAm: false, todayPm: false}
现在解决问题:
上面的if 函数将var blueEmpty 和var blueFilled 图像路径返回到视图,一切都回到视图。这一切都有效。我一辈子都搞不懂如何实现另外两个图像路径:var satBlueEmpty 和 var satBlueFilled。
我正在尝试仅将 2 个“sat”图像路径返回到 2 个属性的视图:yesterdayAm 和 yesterdayPm。我一直在尝试使用$scopeif 语句将blueEmpty 和blueFilled 汇集到todayAm,todayPm,并将sat 图像路径变量汇集到yesterdayAm, yesterdayPm 属性。
我尝试过使用 switch 和嵌套的 if 语句来返回正确的值(真/假),但我觉得自己真的很愚蠢,我这辈子都无法解决这个问题。
我对 Javascript 和 Angular 有点陌生,这是我第一个尝试并完全完成的完整应用程序。我认为这里的方向是从Fed 服务中获取正确的值。谁能帮我解决这个问题?我被卡住了。
*编辑
我认为这个嵌套的if 函数可能有效,但无济于事:
$scope.isFed = function(when, which) {}
if (Fed.getInitState(when)) {
if (which === sat) {
return satBlueFilled;
} else {
return blueFilled;
} else {
if (which === notSat);
return satBlueFilled;
} else {
return satBlueEmpty;
}
}
};
*编辑 2
按照@Steve Lang 的建议,我正在努力在Fed.getInitState 中创建一个switch 语句,以将值1、2、3 或4 返回到控制器函数$scope.isFed。到目前为止,我有以下内容:
getInitState: function(when) {
var trueOrFalse = ref[when];
console.log('when: ' + when + ' ' + 'value: ' + value);
switch (trueOrFalse) {
case 'todayAm':
return 1;
case 'todayPm':
return 2;
case 'yesterdayAm':
return 3;
case 'yesterdayPm':
return 4;
}
},
我可以在案例中添加一个& 运算符来控制when 等于哪个值?看来我需要一个嵌套在 switch 中的 if 语句......
【问题讨论】:
-
我在这个问题中没有看到与 Firebase 或 AngularFire 的任何联系。
-
上面的视图使用了 ng-src 和 ng-click 指令。数据来自 Firebase。
标签: javascript angularjs if-statement javascript-objects