2017-11-16 6 views
0

コントローラからのデータでフォームをあらかじめ入力しようとしています。コントローラからのデータに基づくAngularJSのプレフィックス入力フィールド

シンプルなコード

<div ng-app="app" ng-controller="MainCtrl"> 
    <input name="widget.title" ng-model="widget.title"> 
    <input name="widget.content" ng-model="widget.content"> 
    <button ng-click="set('foo')">set to foo</button> 
</div> 

angular.module('app', []) 
    .controller('MainCtrl', function($scope) { 
     $scope.widget = {title: 'abc'}; 
     $scope.widget = {content: 'test'}; 
     $scope.set = function(new_title) { 
      this.widget.title = new_title; 
     } 
    }); 

のように見えるが、それは常に前を埋めるだけで、最後の入力欄

JSFiddle:http://jsfiddle.net/b40nLuf2/

答えて

1

、あなたは二($scope.widget = {content:'test'})との最初の$scope.widget ($scope.widget = {title:'abc'})を上書きします。

あなたはこのように、二つの属性、タイトルと内容を一つのオブジェクト$scope.widgetを定義する必要があります。

angular.module('app', []) 
    .controller('MainCtrl', function($scope) { 
     $scope.widget = { 
      title: 'abc', 
      content: 'test' 
     }; 
     $scope.set = function(new_title) { 
      this.widget.title = new_title; 
     } 
    }); 
1

あなたは、このコードスニペットを試すことができます

angular.module('app', []) 
    .controller('MainCtrl', function($scope) { 
     $scope.widget = {title: 'abc',content: 'test'}; 
     $scope.set = function(new_title) { 
      this.widget.title = new_title; 
     } 
    }); 

JSFIDDLE:あなたのケースではclick here to see

0

あなたはこの

$scope.widget = { 
     title: 'abc', 
      content: 'test' 
}; 
のようなあなたのオブジェクトを作成する必要があります
関連する問題