如何使用ng-repeat中的选定输入更新我的对象?

时间:2016-05-10 05:43:32

标签: javascript angularjs object

我有这样的代码

{"xyz":[{"a":1,"b":"","c":""}],
"pqr":["l","m"],
"abc":["1234","5678"]}

<div ng-model="item" ng-repeat="product in xyz">
    <div>{product.a}</div>
    <div>
        <select>
            <option ng-repeat="value in pqr">{{value}}</option>
        </select>
    </div>
    <div>
        <select>
            <option ng-repeat="number in abc">{{number}}</option>
        </select>
    </div>
</div>

我想在更改下拉列表中的值时更新我的​​对象并更新onject“xyz”!

2 个答案:

答案 0 :(得分:1)

试试这样。

你应该为select标签定义模型。

&#13;
&#13;
var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
  
    $scope.data = 
        {
      
         "xyz":[{"a":1,"b":"","c":""}],
         "pqr":["l","m"],
         "abc":["1234","5678"]
       }
  
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   <div ng-repeat="(key,value) in data.xyz">
    <div ng-repeat="(k,v) in value">{{v}}</div>
    <div>
        <select ng-model="model1" ng-options="v1 as v1 for v1 in data.pqr" ng-change="value.b = model1">
        </select>
    </div>
    <div>
        <select ng-model="model2" ng-options="number as number for number in data.abc" ng-change="value.c = model2">
        </select>
    </div>
</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

首先,请不要ng-repeat使用<option>使用ng-options代替<select ng-model="product.a" ...> //or which ever property you are trying to update 。可以在此处找到文档和示例:https://docs.angularjs.org/api/ng/directive/ngOptions

至于你的代码,你需要将select绑定到你的对象:

$ git push heroku master
Everything up-to-date

Acer-PC@ZACH /c/lowercase_test (master)
$ heroku logs --tail
2016-05-10T05:39:12.866718+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:54:in `run_initializers'
2016-05-10T05:39:12.866744+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
2016-05-10T05:39:12.866746+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
2016-05-10T05:39:12.866729+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2016-05-10T05:39:12.866731+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
2016-05-10T05:39:12.866735+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
2016-05-10T05:39:12.866745+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
2016-05-10T05:39:12.866744+00:00 app[web.1]:    from /app/config.ru:in `<main>'
2016-05-10T05:39:12.866728+00:00 app[web.1]:    from /app/config/environment.rb:5:in `<top (required)>'
2016-05-10T05:39:12.866730+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
2016-05-10T05:39:12.866733+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
2016-05-10T05:39:12.866732+00:00 app[web.1]:    from /app/config.ru:3:in `block in <main>'
2016-05-10T05:39:12.866748+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/server.rb:61:in `app'
2016-05-10T05:39:12.866749+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/server.rb:80:in `start'
2016-05-10T05:39:12.866753+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
2016-05-10T05:39:12.866751+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
2016-05-10T05:39:12.866750+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
2016-05-10T05:39:12.866754+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
2016-05-10T05:39:12.866746+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
2016-05-10T05:39:12.866753+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:75:in `server'
2016-05-10T05:39:12.866747+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
2016-05-10T05:39:12.866749+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:272:in `start'
2016-05-10T05:39:12.890806+00:00 app[web.1]: => Booting WEBrick
2016-05-10T05:39:12.866748+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
2016-05-10T05:39:12.866861+00:00 app[web.1]:    from bin/rails:4:in `require'
2016-05-10T05:39:12.890824+00:00 app[web.1]: => Run `rails server -h` for more startup options
2016-05-10T05:39:12.890823+00:00 app[web.1]: => Rails 4.2.5.1 application starting in production on http://0.0.0.0:45855
2016-05-10T05:39:12.890825+00:00 app[web.1]: => Ctrl-C to shutdown server
2016-05-10T05:39:12.866862+00:00 app[web.1]:    from bin/rails:4:in `<main>'
2016-05-10T05:39:12.890839+00:00 app[web.1]: Exiting
2016-05-10T05:39:13.784492+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-10T05:39:13.785474+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-10T05:39:13.766179+00:00 heroku[web.1]: Process exited with status 1
2016-05-10T05:39:16.241734+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 25411 -e production`
2016-05-10T05:39:20.549652+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require': /app/config/environments/production.rb:82: syntax error, unexpected => (SyntaxError)
2016-05-10T05:39:20.549702+00:00 app[web.1]: ...                               ^
2016-05-10T05:39:20.549695+00:00 app[web.1]: ...efault_url_options = { host: => "https://lowercase-test.hero...
2016-05-10T05:39:20.549707+00:00 app[web.1]: /app/config/environments/production.rb:82: syntax error, unexpected '}', expecting keyword_end
2016-05-10T05:39:20.549721+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `each'
2016-05-10T05:39:20.549719+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
2016-05-10T05:39:20.549718+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
2016-05-10T05:39:20.549720+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2016-05-10T05:39:20.549727+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `block in <class:Engine>'
2016-05-10T05:39:20.549720+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/engine.rb:598:in `block (2 levels) in <class:Engine>'
2016-05-10T05:39:20.549729+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
2016-05-10T05:39:20.549731+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
2016-05-10T05:39:20.549727+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `instance_exec'
2016-05-10T05:39:20.549728+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `run'
2016-05-10T05:39:20.549728+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:55:in `block in run_initializers'
2016-05-10T05:39:20.549730+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
2016-05-10T05:39:20.549730+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:420:in `block (2 levels) in each_strongly_connected_component_from'
2016-05-10T05:39:20.549731+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:419:in `block in each_strongly_connected_component_from'
2016-05-10T05:39:20.549732+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `each'
2016-05-10T05:39:20.549732+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `tsort_each_child'
2016-05-10T05:39:20.549733+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:413:in `call'
2016-05-10T05:39:20.549733+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:413:in `each_strongly_connected_component_from'
2016-05-10T05:39:20.549734+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each'
2016-05-10T05:39:20.549734+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `call'
2016-05-10T05:39:20.549734+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
2016-05-10T05:39:20.549735+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
2016-05-10T05:39:20.549736+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
2016-05-10T05:39:20.549736+00:00 app[web.1]:    from /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
2016-05-10T05:39:20.549737+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/initializable.rb:54:in `run_initializers'
2016-05-10T05:39:20.549749+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2016-05-10T05:39:20.549737+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/application.rb:352:in `initialize!'
2016-05-10T05:39:20.549749+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
2016-05-10T05:39:20.549745+00:00 app[web.1]:    from /app/config/environment.rb:5:in `<top (required)>'
2016-05-10T05:39:20.549750+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
2016-05-10T05:39:20.549759+00:00 app[web.1]:    from /app/config.ru:in `<main>'
2016-05-10T05:39:20.549758+00:00 app[web.1]:    from /app/config.ru:in `new'
2016-05-10T05:39:20.549750+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
2016-05-10T05:39:20.549759+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
2016-05-10T05:39:20.549751+00:00 app[web.1]:    from /app/config.ru:3:in `block in <main>'
2016-05-10T05:39:20.549752+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
2016-05-10T05:39:20.549752+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
2016-05-10T05:39:20.549761+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
2016-05-10T05:39:20.549760+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
2016-05-10T05:39:20.549762+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/server.rb:61:in `app'
2016-05-10T05:39:20.549762+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
2016-05-10T05:39:20.549763+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
2016-05-10T05:39:20.549763+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:272:in `start'
2016-05-10T05:39:20.549760+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
2016-05-10T05:39:20.549766+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/server.rb:80:in `start'
2016-05-10T05:39:20.549767+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
2016-05-10T05:39:20.549766+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
2016-05-10T05:39:20.549767+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:75:in `server'
2016-05-10T05:39:20.549770+00:00 app[web.1]:    from bin/rails:4:in `<main>'
2016-05-10T05:39:20.549768+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
2016-05-10T05:39:20.549768+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
2016-05-10T05:39:20.549769+00:00 app[web.1]:    from bin/rails:4:in `require'
2016-05-10T05:39:20.572114+00:00 app[web.1]: => Booting WEBrick
2016-05-10T05:39:20.572144+00:00 app[web.1]: => Rails 4.2.5.1 application starting in production on http://0.0.0.0:25411
2016-05-10T05:39:20.572145+00:00 app[web.1]: => Run `rails server -h` for more startup options
2016-05-10T05:39:20.572146+00:00 app[web.1]: => Ctrl-C to shutdown server
2016-05-10T05:39:20.572171+00:00 app[web.1]: Exiting
2016-05-10T05:39:21.490533+00:00 heroku[web.1]: Process exited with status 1
2016-05-10T05:39:21.487607+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-10T05:39:52.664032+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lowercase-test.herokuapp.com request_id=5a78b3bc-0ec4-4c9e-ace7-68bddb175919 fwd="70.181.171.158" dyno= connect= service= status=503 bytes=
2016-05-10T05:39:56.400679+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lowercase-test.herokuapp.com request_id=6a4b7144-2cd4-408f-ae30-9cf718bd5bc4 fwd="70.181.171.158" dyno= connect= service= status=503 bytes=
2016-05-10T05:45:12.150438+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lowercase-test.herokuapp.com request_id=9b13cd97-b2ba-4429-be2c-d9038181e569 fwd="70.181.171.158" dyno= connect= service= status=503 bytes=
2016-05-10T05:46:39.512933+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lowercase-test.herokuapp.com request_id=dd9a90fe-2c5a-486e-a78b-975bdce7193c fwd="70.181.171.158" dyno= connect= service= status=503 bytes=