0

次のようなことを行って、テンプレート angularjs にリンクを作成しようとしています。

 <a ng-href="/#!/content/[[value.id]]">[[key]]</a>

しかし、symfony2 のようなことを行うことが可能かどうか疑問に思っています。例:

ルーティング.yml

  home_redirect:
    path: /
    defaults:
        _controller: FrontendBundle:Controller:function
        path: /home
        permanent: true
    options:
        expose: true

そして、それを小枝テンプレートで使用するには、次のようにします。

<a href="{{ path('home_redirect')}}"> one link to home </a>

すべてのルートを「ハードコード」する必要がないので、これは本当に助かります。

4

2 に答える 2

2

適切なルーティングを確保するには、ui-router を使用できます。

これがプランカーの例です

これがどのように機能するか:

1 - github のインストール ガイドに従ってください

2 - 状態の定義を書きます:

app.config(function($stateProvider, $urlRouterProvider){
  //If no route match, you'll go to /index
  $urlRouterProvider.otherwise('/index');

  //my index state
  $stateProvider
  .state('index', {
    url: '/index',
    templateUrl: 'index2.html',
    controller: 'IndexCtrl'
  })

  //the variable state depending on an url element
  .state('hello', {
    //you will be able to get name with $stateParams.name
    url: '/hello/:name',
    templateUrl: 'hello.html',
    controller: 'HelloCtrl'
  })  
});  

3 - 州名でリンクを書きます :

//add this directive to an html element
//This will go to /index
ui-sref="index"
//This will go to /hello/
ui-sref="hello"
//This will go to /hello/ben
ui-sref="hello({name:'ben'})"
//This will go to /hello/{myname}
ui-sref="hello({name:myname})"

4 - パラメータをコントローラーに取得します。

//inject $stateParams
app.controller('HelloCtrl', function($scope, $stateParams){
  $scope.controller = "IndexCtrl";
  //get the param name like this
  $scope.name = $stateParams.name;
});

それが役に立ったことを願っています。また、ui-router には、resolve やネストされた状態/ビューなどの非常に強力なツールがあることにも注意してください。あなたはおそらく今、または後で論文を必要とするでしょう。

PS : プランカーが機能しない場合は、フォークして再度保存してください。

于 2015-07-09T15:32:08.550 に答える
1

あなたはこれを行うことができます:

'use strict';

angular.module('AngularModule')
    .config(function ($stateProvider) {
        $stateProvider
            .state('YourStateName', {
                url: '/your/url',
                views: {
                    'aViewName': {
                        templateUrl:'views/components/templates/yourTemplate.html',
                        controller: 'YourController'
                    }
                },
                resolve: {

                }
            });
    });


// then in your controller

angular.module('AngularModule')
.controller('MyController',function($scope, $state){
$scope.goTo = function(){
$state.go('YourStateName');
}
}

);

//in your html make sure the <a> tag is in scope with the 'MyController'

<a ng-click='goTo'>[[key]]</a>

また

これを行うことができます:

<a ng-href="/your/url"></a>

そうすれば、コントローラーをバイパスしても、状態で指定されたコントローラーにロジックを配置できます

于 2015-07-09T15:20:54.267 に答える