我想使用ng-style基于它们的值有条件地设置表中数据元素的颜色.使用ng repeat生成每行数据.
所以我有类似的东西:
<tr ng-repeat="payment in payments">
<td ng-style="set_color({{payment}})">{{payment.number}}</td>
Run Code Online (Sandbox Code Playgroud)
和我的控制器中的功能,如下所示:
$scope.set_color = function (payment) {
if (payment.number > 50) {
return '{color: red}'
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过几种不同的东西.甚至将颜色设置为支付对象中的数据属性,但似乎我无法从数据绑定中处理数据,有没有人知道我可以使这个工作的方式?谢谢.
如果我有一个带有多个条件语句的函数,其中每个分支都被执行从函数返回.我应该使用多个if语句,还是使用/ elif/else?例如,假设我有一个功能:
def example(x):
if x > 0:
return 'positive'
if x < 0:
return 'negative'
return 'zero'
Run Code Online (Sandbox Code Playgroud)
写作更好:
def example(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
Run Code Online (Sandbox Code Playgroud)
两者都有相同的结果,但是比另一个更有效或被认为更惯用?
编辑:
有几个人说在第一个例子中,if语句总是被评估,这对我来说似乎并非如此
例如,如果我运行代码:
l = [1,2,3]
def test(a):
if a > 0:
return a
if a > 2:
l.append(4)
test(5)
Run Code Online (Sandbox Code Playgroud)
我仍然会等于[1,2,3]
我正在编写一个ETL过程来从产品数据库中读取事件级数据,转换/聚合它并写入分析数据仓库.我正在使用clojure的core.async库将这些进程分成并发执行的组件.这是我的代码的主要部分现在看起来像
(ns data-staging.main
(:require [clojure.core.async :as async])
(:use [clojure.core.match :only (match)]
[data-staging.map-vecs]
[data-staging.tables])
(:gen-class))
(def submissions (make-table "Submission" "Valid"))
(def photos (make-table "Photo"))
(def videos (make-table "Video"))
(def votes (make-table "Votes"))
;; define channels used for sequential data processing
(def chan-in (async/chan 100))
(def chan-out (async/chan 100))
(defn write-thread [table]
"infinitely loops between reading subsequent 10000 rows from
table and ouputting a vector of the rows(maps)
into 'chan-in'"
(while true
(let [next-rows (get-rows table)]
(async/>!! chan-in next-rows)
(set-max table (:max-id …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我正在编写的 clojure 程序中实现带有记录的协议。我得到的错误是“不支持的绑定形式”。
(defprotocol query-rows
(query-text [table])
(trans-cols [table rows])
(set-max [table] [table id]))
(defrecord Submissions [data max-id]
query-rows
(query-text [table]
(gen-query-text "SubmissionId" "Valid" "Submission"))
(trans-cols [table rows]
(let
[trans-data
(->>
rows
(trans-col #(if % 1 0) :valid :valid_count)
(trans-col #(if % 0 1) :valid :non_valid_count)
(trans-col format-sql-date :createdon :date))]
(assoc table :data trans-data)))
(set-max
([table]
(when-let [id (gen-get-max "SubmissionAgg2")]
(assoc table :max-id id)))
([table id] (assoc table :max-id id))))
Run Code Online (Sandbox Code Playgroud)
“set-max”函数是引发错误的原因。我有一种感觉,我试图错误地使用多个 arity。有谁知道我做错了什么?
我正在尝试编写一个宏,我可以使用它来调用新线程上的函数并在运行它之后打印函数的名称以及线程的名称.
到目前为止我所拥有的是:
(defmacro start-threads [f]
'(let [t (Thread. 'f)]
(prn (str "running " 'f " on: " (.getName t)))))
Run Code Online (Sandbox Code Playgroud)
我跑的时候:
(start-threads funcname)
Run Code Online (Sandbox Code Playgroud)
输出:"运行f:螺纹-47".我希望它输出:"运行funcname on:Thread-47.当我尝试取消它时它会尝试评估该函数.我知道我没有在线程上运行.start,但我应该能够添加在那之后.我确定宏在这里不是完全必要的,我主要是出于好奇而感到好奇,因为我刚刚开始关注clojure中的宏是如何工作的.
我正在构建一个使用Angularjs的Web应用程序.在我的一个控制器中,我调用后端,返回用户的当前登录状态.
$http.get('http://localhost:3000/status')
.success(function(status) {
if (status == 'internal') {
// do stuff...
Run Code Online (Sandbox Code Playgroud)
即使请求的响应状态等于'internal',(status =='internal')也会计算为false.为了测试这一点我放了
$http.get('http://localhost:3000/status')
.success(function(status) {
console.log(typeof status)
console.log(status)
console.log(status == "internal")
Run Code Online (Sandbox Code Playgroud)
打印
string
"internal"
false
Run Code Online (Sandbox Code Playgroud)
关于什么可以在这里拧什么的想法?
clojure ×3
angularjs ×2
asynchronous ×1
conditional ×1
etl ×1
goroutine ×1
idioms ×1
javascript ×1
jvm ×1
macros ×1
ng-style ×1
python ×1