On Github destan / angularjs-sunum
Destan Sarpkaya @ KodGemisi
Öncelikle neden bir Javascript uygulama çatısına gereksinim var ondan bahsedelim.
jQuery neyimize yetmiyor?
Teknik kısıt yok ama ...
Verimlilik ve sürdürülebilirlik önemli
“İnsanlar için kod yazın, makinalar için değil.”function ProfileCtrl($scope) { $scope.reset = function() { $scope.user = {}; }; $scope.getUser = function() { $scope.user = {...}; } }
<div ng-app=""> <div class="well" ng-controller="ProfileCtrl"> <button class="btn" ng-click="getUser()">Kullanıcıyı getir</button> <button class="btn" ng-click="reset()">Reset</button> <div ng-if="!user.skills">Bir şey yok...</div> <ul class="well" ng-if="user.skills"> <li ng-repeat="skill in user.skills" class="{{skill.type}}">{{skill.name}}</li> </ul> </div> </div>
<div ng-app=""> <div class="well"> <label>Ad:</label> <input class="form-control" type="text" ng-model="adiniz" placeholder="Adınızı yazın"> <h1>Selam {{adiniz}}!</h1> </div> </div>
<div ng-app="myApp"> ... </div>
angular.module('myApp', ['ngResource', 'videoPlayer'])
angular.module('myApp', ['ngResource', 'videoPlayer']) .constant('ENDPOINT', 'https://api.kodgemisi.com/v2/') .factory('UserSyncService', ['$scope', 'ENDPOINT', function($scope, ENDPOINT){ ... }]) .controller('ProfileCtrl', ['$scope', 'UserSyncService', function($scope, userSyncService){ ... }])
<div ng-app=""> <div> <input type="text" ng-model="data"> <h3>Root scope: {{data}}</h3> </div> <div ng-controller="FirstCtrl"> <input type="text" ng-model="data"> <h3>1. Controller: {{data}}</h3> </div> <div ng-controller="SecondCtrl"> <input type="text" ng-model="data"> <h3>2. Controller: {{data}}</h3> </div> </div>
angular.module('SampleForDirective', []) .directive('toggle', function() { return function(scope, element, attrs) { element.addClass('btn-success').text('Açık'); var on = true; element.on('click', function() { if(on) element.removeClass('btn-success').addClass('btn-danger').text('Kapalı'); else element.removeClass('btn-danger').addClass('btn-success').text('Açık'); on = !on; }); }; });
<button class="btn" toggle=""></button>
<button class="btn" toggle=""></button> <button class="btn toggle"></button> <toggle></toggle>
describe('Test showcase', function() { describe('ProfileCtrl', function(){ it('should instantiate "users" model with 2 users', function() { var scope = {}; var ctrl = new ProfileCtrl(scope); expect(scope.users.length).toBe(2); }); }); });
$ karma start karma-config.js INFO [karma]: Karma v0.10.5 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome INFO [Chromium 30.0.1599 (Ubuntu)]: Connected on socket GzV3EwULE_yqFCU5elUh Chromium 30.0.1599 (Ubuntu): Executed 1 of 1 SUCCESS (0.14 secs / 0.023 secs)
Chromium 30.0.1599 (Ubuntu) Test showcase ProfileCtrl should instantiate "users" model with 2 users FAILED Expected 0 to be 2. Error: Expected 0 to be 2. at null.<anonymous> (/home/destan/dev/...samples/tests.js:9:34) </anonymous>
$.get('http://api.kodgemisi.com/v2/user/35', function(data) { $scope.user = data; });
$.get('http://api.kodgemisi.com/v2/user/35', function(data) { $scope.$apply(function(){ $scope.user = data; }); });
$http('http://api.kodgemisi.com/v2/user/35') .success(function(data, status) { $scope.user = data; })
<div>{{user.name}}</div>
<div ng-bind="user.name"></div>