0

オブジェクトに基づいて動的フォームを作成しようとしています。例: 生成された選択ボックスにオプションが含まれるようにします。

マークアップ:

<select ng-repeat="select in selects"
        ng-init="options = refactor.options(select)"
        ng-options="option in options">
</select>

コントローラ:

myApp.controller('MyCtrl', function($scope, refactor) {
   $scope.refactor = refactor;
   $scope.selects = [
          { text: "Country", value="chosen.country", options=["France", "England"] },
          { text: "Gender", value="chosen.gender", options="/gender" }
        ]
});

工場:

myApp.factory('refactor', function($scope, $http) {
  return {
    options: function(select) {
      if(typeof(select.options) === 'object') { return select.options };
      // otherwise assume select.options is 
      // a path to fetch options by ajax:
      $http.get(select.options).success(function(data) {
         select.options = data; // data == ['male', 'female']
      });

      return []; // placeholder until promise is fulfilled
    }
  }
})

データ ( $scope.selects ) は期待どおりに更新されますが、DOM は更新されません。これはおそらく、変更に応じて refactor.options() が再度呼び出されていないことを意味します。スコープ オブジェクトをファクトリに渡して $apply を呼び出して更新を強制しようとしましたが、うまくいきません。

私は何が欠けていますか?

4

2 に答える 2

0

これを試してみてください

myApp.factory('refactor', function($scope, $http) {

return {
    options: function(select) {
               var menuItems={
                     options:[]
                  }

                if(typeof(select.options) === 'object'){
                   menuItems.options=select.options
                };

               // otherwise assume select.options is 
               // a path to fetch options by ajax:

               $http.get(select.options).success(function(data) {
                    select.options = data; 
                    menuItems.options=data;// data == ['male', 'female']
               });

               return menuItems; // placeholder until promise is fulfilled
            }
       }
});

次に、ビュー内では次のようになります

<select ng-repeat="select in selects"
    ng-init="menuItems= refactor.options(select)"
    ng-options="option in menuItems.options">
</select>
于 2015-06-21T09:55:14.350 に答える
0

ng-init="data = refactor.options(select)"データからデータを取得した後、 soを使用する必要がありajaxます で埋められ、代わりに as をoptions使用できます。ng-optionsng-options="option in data.options"

マークアップ

<select ng-repeat="select in selects"
    ng-init="data = refactor.options(select)"
    ng-options="option in data.options">
</select>
于 2015-06-20T17:23:25.000 に答える