Skip to content

Dev.front js angular

nothing edited this page Jan 14, 2014 · 14 revisions

Assign value

  1. phtml value assign to ng html template
//phtml
angular
  .module('<?php echo $module; ?>', ['ngRoute', 'pi'])
  .constant('config', {
    //Translate ng template
    t: {
       key1: <?php echo _a('value1'); ?>
    },
    //phtml data assign to module config
    data: {
      list: <?php echo json_encode($list); ?>
    }
  });
//{{module}}/asset/admin/{{controller}}.js
angular.module('{{module}}')
.controller('privacyCtrl', ['$scope', 'config',
   angular.extend($scope, config.data);
]);
//{{module}}/public/ng-template/admin/{{controller-action.html}}
<tr ng-repeat="item in list">
 <td>{{item.title}}
 <td>{{item.description}}
  1. Load data through ajax
//php
IndexAction() {
 return array(
    'list' => array()
 )
}
//Route
.when('/index', {
      templateUrl: tpl('index-index'),
      controller: 'privacyCtrl',
      resolve: {
        data: ['$q', '$rootScope', 'server',
          function($q, $rootScope, server) {
            var deferred = $q.defer();
            //Loading
            $rootScope.alert = 2;
            //Ajax request data
            server.getData().success(function(data) {
              //Assign responsive data to controller
              deferred.resolve(data);
              //Clear loading message
              $rootScope.alert = '';
            });
            return deferred.promise;
          }
        ]
      }
    })
//Controller
.controller('privacyCtrl', ['$scope', 'server', 'data',
  function($scope, server, data) {
    angular.extend($scope, data);
  }
]);
//{{module}}/public/ng-template/admin/{{controller-action.html}}
<tr ng-repeat="item in list">
 <td>{{item.title}}
 <td>{{item.description}}
Clone this wiki locally