ui-router のビューから状態にカスタムデータを渡すにはどうすればよいですか? 質問する

ui-router のビューから状態にカスタムデータを渡すにはどうすればよいですか? 質問する

私は$stateProviderルート設定に を使用しています。 提供されているカスタム データを利用して、ある部分ページから別の部分ページ ( ng-click) にカスタム データを渡したいと思っています。

これは私がこれまでに得た最高のものです:

状態オブジェクトにカスタムデータを添付する

状態オブジェクトにカスタム データを添付できます (競合を避けるため、データ プロパティを使用することをお勧めします)。

// Example shows an object-based state and a string-based state 
var contacts = { 
    name: 'contacts',
    templateUrl: 'contacts.html',
    data: {
        customData1: 5,
        customData2: "blue"
    }
} 
$stateProvider
    .state(contacts)
    .state('contacts.list', {
        templateUrl: 'contacts.list.html',
        data: {
            customData1: 44,
            customData2: "red"
        }
    }) 

上記の例では、次のようにデータにアクセスできます。

function Ctrl($state){
     console.log($state.current.data.customData1) // outputs 5;
     console.log($state.current.data.customData2) // outputs "blue";
 }

ソース

独自のテンプレートとコントローラーを持つ、customers という別の状態があるとします。customers コントローラー/ビュー内の contacts の状態データ オブジェクトの値を変更するにはどうすればよいでしょうか? つまり、次のように変更したいのです:

data: {
    customData1: 44,
    customData2: "red"
} 

これに:

data: {
    customData1: 100,
    customData2: "green"
} 

何かご指摘やサンプルがあれば、ぜひ教えてください!

修正 - 自分で動作させることができました。方法は次のとおりです。コントローラー (customerCtrl など) では、名前で連絡先の状態を取得し、次のようにカスタム データ オブジェクトのプロパティ値を更新するなど、必要な変更を加えることができます。

 //get the state by name and change the value of custom data property

 $state.get('contacts').data.customData1= 100;
 $state.go('contacts'); // then you can make a go to that state.

ベストアンサー1

私は自分でそれを動作させました。その方法は次のとおりです。コントローラ(customerCtrlなど)では、連絡先の状態を名前で取得できます(https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statename$state.get([stateName]) を探します)
状態を取得したら、次のようにカスタム データ オブジェクトのプロパティ値を更新するなど、必要な変更を加えることができます。

//get the state by name and change the value of custom data property 
 $state.get('contacts').data.customData1= 100;
  // then you can go to that state.
 $state.go('contacts');

これが誰かの役に立つことを願っています。

おすすめ記事