我有这样的服务:
app.service('Utilities', function() {
this.sum = function(items, prop) {
var count, total;
total = 0;
count = 0;
if (items === null) {
total = 0;
}
while (count < items.length) {
total += items[count][prop] * 1 || 0;
count++;
}
return total;
};
});
Run Code Online (Sandbox Code Playgroud)
和这样的控制器:
app.controller('writeCtrl', function($scope, Utilities, students) {
$scope.students = students;
$scope.total_age = Utilities.sum($scope.students, 'age');
});
Run Code Online (Sandbox Code Playgroud)
而且我一直在收到错误
Typerror:Utilities.sum不是一个函数
这是令人困惑的,因为公用事业服务下的十几个其他功能工作正常.是什么导致了这个问题,我该如何让这个功能起作用?
编辑 实际Coffeescript版本
app.service 'Utilities', ->
@sum = (items, prop) ->
total = 0
count = 0
if items …
Run Code Online (Sandbox Code Playgroud) 我想通过Activeadmin的formtastic制作动态选择选项,如下所示:
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
#selects user from list. WORKING
f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user)
#collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons.
f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user)
#collection of lessons will change to reflect lessons connected by chosen user and student. NOT …
Run Code Online (Sandbox Code Playgroud) 我有两个数组,像这样的用户和雇员:
Users = [{id:1, name: "ryan"}, {id:2, name:"Julie"}]
Employments = [{user_id: 1, title: "manager"}, {user_id: 2, title: "Professor"}]
Run Code Online (Sandbox Code Playgroud)
我想在ng-repeat中显示Employments数组,如下所示:
<li ng-repeat="employment in Employments">
{{employment.user.name}}
</li>
Run Code Online (Sandbox Code Playgroud)
如何将Users数组映射到Employments数组?
我有一个像这样的产品专栏:
t.decimal :price, :precision => 12, :scale => 2
Run Code Online (Sandbox Code Playgroud)
并且它保持返回500.0而不是预期的两位小数500.00.然后我尝试通过控制台手动更改产品价格,它只会节省1位小数.
>product.price = 500.00
=> 500.0
>product.price
=> #<BigDecimal:7ff479c6ba40,'0.5E3',9(36)>
Run Code Online (Sandbox Code Playgroud)
如何获取十进制列以保存并返回两位小数?