JS篇 AngularJS <<The complete guide of AngularJS>>笔记

指令:

angular.module('myApp', [])
.directive('myDirective', function() {
    return {
        restrict: 'A',
     // 定义一个isolated scope
scope: {} }; }) .directive(
'myInheritScopeDirective', function() { return { restrict: 'A',
     // 新的子scope继承parent scope scope:
true }; })
.directive('myInheritScopeDirective', function() {
    return {
        restrict: 'A',
     // 使用指令元素节点上用其他指令(如:ng-controller)定义的scope scope: false }; })
 

 ng-init: 在当前DOM节点所属的scope上操作,最外层的scope为:$rootScope。

定义服务的多种方式:

// factory方式
angular.module('myApp', []).factory('UserService', function($http) { var current_user; return { getCurrentUser: function() { return current_user; }, setCurrentUser: function(user) { current_user = user; } } });
// service: contructor方式
var Person = function($http) {
    this.getName = function() {
        return $http({
            method: 'GET',
            url: '/api/user'
        });
    };
};
angular.service('personService', Person);
原文地址:https://www.cnblogs.com/diydyq/p/4177353.html