0

angularJSの新機能ですが、以下のコード間の長所と短所を知りたいですか? 使用するのに推奨されるのはどれですか?

$routeProvider.when('foo', {
    templateUrl: 'foo.html',
    controller: fooCtrl

    function fooCtrl() {
        //something here
    }
});

また

$routeProvider.when('foo', {
    templateUrl: 'foo.html'
});

app.controller("fooCtrl", function() {
    //something here
});

//in html
<div ng-controller="fooCtrl"></div>
4

2 に答える 2

0

私は 2 番目のアプローチを好み、アプリケーションを開発するときに使用します。これは、コントローラーからルート構成、モジュール配線などを分離する、エレガントなコーディング方法です。私たちはメインファイルにルート構成を書くことができますapp.coffee [私はcoffeescriptを使用します]のように定義します

routesConfig = ($route) ->
    $route.when('/employees',
        {templateUrl: 'employee.employeeView.html'})

ここで、routesconfig とワイヤリング モジュール [例: employee.employeeController] を定義します。

modules = ['employee.employeeController', 'user.userController']

ここからangularアプリケーションを作成して開始できます。

m = angular.module('app', modules)
m.config['$route', routesConfig]

たとえば、 employeeController.coffeeでコントローラーを個別に指定できるようになりました。

name = 'employee.employeeController'
mod = angular.module(name, [])
mod.controller(name, [
    '$scope'
    '$log'
    ($scope, $log) ->
          $scope.name = 'java'

あなたのビューで、employeeView.htmlと言います

<div ng-controller="employee.employeeController">
 <div class ="info">
  Name is {{name}} 
</div>

基本的に、コントローラー、ビュー、アプリケーション構成を互いに分離しました。

于 2013-04-01T15:09:17.587 に答える