如何将函数返回值或动态数据设置为角度状态自定义数据

时间:2017-01-23 09:15:00

标签: javascript angularjs jhipster

我在一些角度代码中遇到麻烦,我想将动态数据设置为角状态自定义数据。这是我的代码

function stateConfig($stateProvider) {
        $stateProvider
        .state('apps', {
            parent: 'entity',
            url: '/apps',       
            data: {
                authorities: ['ROLE_USER'],//i want to set dynamic data or return data here
                pageTitle: 'smsApp.apps.home.title'                     
            },           
            views: {
                 'nav@':{
                    templateUrl: 'app/layouts/navbar/navbar.html',
                    controller: 'NavbarController',
                    controllerAs: 'vm'
                },'leftnav@': {
                    templateUrl: 'app/layouts/leftnavbar/leftnavbar.html',
                    controller: 'LeftNavbarController',
                    controllerAs: 'vm'
                },
                'content@': {
                    templateUrl: 'app/entities/apps/apps.html',
                    controller: 'AppsController',
                    controllerAs: 'vm'
                }
            },
            resolve: {
                translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
                    $translatePartialLoader.addPart('apps');
                    $translatePartialLoader.addPart('global');
                    return $translate.refresh();
                }]

            }
        })

在代码上方,我想在

中设置动态函数或数据
data: {
                authorities: dynamic data or funciton,//i want to set dynamic data or return data here
                pageTitle: 'smsApp.apps.home.title'                     
            }

所以,有人会帮助我知道那个人

由于

1 个答案:

答案 0 :(得分:0)

我想你应该声明一个返回data的匿名函数。例如:

int prevPosition = 0; // first item position 0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager viewPager = (ViewPager) findViewById(R.id.vp);
    final Adapter adapter = new Adapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            // when moves from one view to another on swiping
            TestFragment testFragment = (TestFragment) adapter.getRegisteredFragment(prevPosition);
            prevPosition = position;
            insertDataIntoTable(testFragment.getEditTextData());
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

编辑:如果您希望在路线声明中自动提取数据,您可以这样:

data: {
            authorities: function(){ return data }
            pageTitle: 'smsApp.apps.home.title'                     
        },