2015年11月27日 星期五

2015年11月26日 星期四

angularjs factory return problem null undefined




========== work =========

  factory.getInit = function(type,lable){
    var t=null;
    angular.forEach(factory.models[type], function(value, key) {
        if(value.lable == lable){
          t=value.init;
          console.log("getInit-value.init: "+t);
        }
    });
    return t;
  };

========== doesn't work =========


  factory.getInit = function(type,lable){
    angular.forEach(factory.models[type], function(value, key) {
        if(value.lable == lable){
          console.log("getInit-value.init: "+value.init);
          return value.init;
        }
    });
  };


2015年11月22日 星期日

2015年11月19日 星期四

angularjs date

https://docs.angularjs.org/api/ng/filter/date

<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
    <span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
   <span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'MM/dd/yyyy @ h:mma'}}</span>:
   <span>{{'1288323623006' | date:'MM/dd/yyyy @ h:mma'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:"MM/dd/yyyy 'at' h:mma"}}</span>:
   <span>{{'1288323623006' | date:"MM/dd/yyyy 'at' h:mma"}}</span><br>



{{1288323623006 | date:'medium'}}Oct 29, 2010 11:40:23 AM
{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}2010-10-29 11:40:23 +0800
{{1288323623006 | date:'MM/dd/yyyy @ h:mma'}}10/29/2010 @ 11:40AM
{{1288323623006 | date:"MM/dd/yyyy 'at' h:mma"}}10/29/2010 at 11:40AM

2015年11月18日 星期三

angularjs , log in redirect ng-router


http://stackoverflow.com/questions/11541695/redirecting-to-a-certain-route-based-on-condition


'use strict';

var app = angular.module('app', [])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: "login.html",
                controller: LoginController
            })
            .when('/private', {
                templateUrl: "private.html",
                controller: PrivateController,
                resolve: {
                    factory: checkRouting
                }
            })
            .when('/private/anotherpage', {
                templateUrl:"another-private.html",
                controller: AnotherPriveController,
                resolve: {
                    factory: checkRouting
                }
            })
            .otherwise({ redirectTo: '/' });
    }]);

var checkRouting= function ($q, $rootScope, $location) {
    if ($rootScope.userProfile) {
        return true;
    } else {
        var deferred = $q.defer();
        $http.post("/loadUserProfile", { userToken: "blah" })
            .success(function (response) {
                $rootScope.userProfile = response.userProfile;
                deferred.resolve(true);
            })
            .error(function () {
                deferred.reject();
                $location.path("/");
             });
        return deferred.promise;
    }
};
=========================


angular.module('yoApp')
  .service('appService', function ($q, $timeout, $rootScope, $location) {


this.isAdmin=function(){
console.log("isAdmin");

       var deferred = $q.defer();

      deferred.resolve(true);
       // deferred.reject();
       // $location.path("/about");

       return deferred.promise;
}
});



  .config(function ($routeProvider) {

    $routeProvider
      .when('/me', {
        template: '<h1> me </h1>',
        resolve: {
            factory: function(appService) {
              return appService.isAdmin();
            }
        }
      })


  });




2015年11月11日 星期三

angular js Dependency injection

https://www.airpair.com/angularjs/posts/top-10-mistakes-angularjs-developers-make


Dependency injection is one of AngularJS's best patterns. It makes testing much simpler

var app = angular.module('app',[]); app.controller('MainCtrl', function($scope, $timeout){ $timeout(function(){ console.log($scope); }, 1000); });
Here, it's very clear that MainCtrl depends on $scope and $timeout.



angularness factory vs service

http://ithelp.ithome.com.tw/question/10161278


Service 是用 new 來建立的


Factory 的行為很簡單,Controller 注入Factory 取得的物件就是被 factory object ({}) 包起來並且 return 的物件
也就是說,假如沒有被 { } 包起來值都不會被外面取得。
(p.s. 不一定要 return object, 任何 type 都可以)

Service 是把東西存在 this 裡面,像是我們平常在製作 constructor 一樣。
以上範例假如寫成 constructor 就會是

2015年11月9日 星期一

2015年11月8日 星期日

angularjs complied order ,structure


http://stackoverflow.com/questions/20663076/angularjs-app-run-documentation
















Here's the calling order:
  1. app.config()
  2. app.run()
  3. directive's compile functions (if they are found in the dom)
  4. app.controller()
  5. directive's link functions (again, if found)
Here's a simple demo where you can watch each one executing (and experiment if you'd like).
============
very good example:
============
index.html:54 app config
index.html:50 app run
index.html:37 directive setup
index.html:39 directive compile
index.html:58 app controller
index.html:45 directive link


        <script>
                var myApp = angular.module('myApp', []);
                myApp.factory('aProvider', function() {
                   console.log("factory");
                });

                myApp.directive("test1", function() {
                    console.log("directive setup");
                    return {
                        compile: function() {console.log("directive compile");}
                    }
                });

                myApp.directive("test2", function() {
                    return {
                        link: function() {console.log("directive link");}
                    }
                });

                myApp.run(function() {
                    console.log("app run");
                });

                myApp.config( function() {
                    console.log("app config");
                });

                myApp.controller('myCtrl', function($scope) {
                    console.log("app controller");
                });
        </script>

================