我正在使用带有 Rails 的 AngularJS 并创建未在 AngularJS 范围内注册的动态嵌套表单项。他们被分配了“ng-dirty”类。
我有一个投资者,他有三间房子,在我的 Angular 控制器中,我只分配前 2 间,因为我真的想通过 Rails 设置它们。
$ (event) ->
app = angular.module "investor", []
app.controller("InvestorCtrl", ["$scope", ($scope) ->
$scope.houses = [
{ cost: "295000", value: "450000" },
{ cost: "600000", value: "620000" }
]
$scope.calc_totals = ->
# Initialise variables
cost = 0
value = 0
# Go through each house and calculate the total cost
for h in $scope.houses
cost = parseInt(cost) + parseInt(h.cost)
value = parseInt(value) + parseInt(h.value)
# Set the total
$scope.total_cost = cost
$scope.total_value = value
# Run the calculation at the start
# $scope.calc_totals()
])
angular.bootstrap document, ['investor']
这是我的表格
%div{"ng-controller" => "InvestorCtrl"}
= form_for(@investor) do |f|
- if @investor.errors.any?
#error_explanation
%h2
= pluralize(@investor.errors.count, "error")
prohibited this investor from being saved:
%ul
- @investor.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name, "ng-model" => "name"
- i = 0
= f.fields_for :houses do |builder|
.field
= render :partial => "house", :locals => {:f => builder, :i => i}
- i = i + 1
.field
= f.label :total_cost
= f.number_field :total_cost, "ng-model" => "total_cost"
.field
= f.label :total_value
= f.number_field :total_value, "ng-model" => "total_value"
.actions
= f.submit
如果我输入第一个或第二个房子的“成本”或“价值”,那么它会更新总成本,但如果我更新第三个房子,它不会更新总成本。
有没有办法动态地告诉 angularjs 监听这些元素而不在控制器中分配它们?