msforest / notebook

好记性不如烂笔头,记录知识的点点滴滴。
https://github.com/msforest/notebook/wiki
0 stars 0 forks source link

angularjs指令--指令创建 #1

Open msforest opened 7 years ago

msforest commented 7 years ago

指令(Directives)是所有AngularJS应用最重要的部分。尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令。

自定义指令

指令可以有四种表现形式,分别为元素(element)、属性(attribute)、注释(comment)、样式类(class),正好代表ECMAScript的前四个字母,还是很好记的。基本样式如下:

var app = angular.module('myapp', []);

app.directive('helloWorld', function() {
  return {
      restrict: 'ECMA',
      replace: true,
      transclude: true,
      template: '<div>Hello World!!</div>'
      //templateUrl: 'hello.html'
  };
});

使用app.directive()方法在模块中注册一个新的指令,第一个参数是指令的名称,第二个参数是返回指令定义对象的函数;如果你的指令依赖于其他的对象或者服务,比如 $rootScope, $http, 或者$compile,他们可以在这个时间被注入。 页面使用形式:

<hello-world></hello-world>
<div class="hello-world"></div>
<!-- directive:hello-world -->
<div hello-world></div>

部分属性说明:


angularjs深度学习参考


Adventure may hurt you, but monotony will kill you. - -也许冒险会让你受伤,但一成不变会使你灭亡。

msforest commented 7 years ago

29 AngularJS Interview Questions – Can You Answer Them All?

1. 对angular的filter过滤器进行单元测试的基本步骤?

2.watches存放最大的数量是多少,怎么保持一个可观的数量?

3.控制器之间怎么传递数据?

4.ng-ifng-show/ng-hide的不同?

5.AngularJS的digest循环是什么?

6.使用AngularJS在哪儿实现DOM操作?

7.混合使用AngularJS和jQuery好还是不好?

8.如果要从1.4迁移到1.5,主要重构的事情是什么?

9.怎么实现单向绑定

10.单向绑定和双向绑定有什么区别

11.解释一下$scope.$apply()是怎么工作的?

12.使用什么指令可以通过移除DOM上的元素而不改变样式来隐藏元素?

13.什么使得angular.copy()如此强大?

14. 怎样使AngularJS的services返回一个promise?

angular.factory('testService', function($q){
    return {
        getName: function(){
            var deferred = $q.defer();

            //API call here that returns data
            testAPI.getName().then(function(name){
                deferred.resolve(name)
            });
            return deferred.promise;
        }
    }
});

15.略

16. 指令的类型都有哪些?如何使用?

17.什么时候使用属性?什么时候使用元素?

18.如何重置$timeout/$interval,如何取消$watch

var customTimeout = $timeout(function () {
  // arbitrary code
}, 55);

$timeout.cancel(customTimeout);
///////
var deregisterWatchFn = $scope.$on(‘$destroy’, function () {
    // we invoke that deregistration function, to disable the watch
    deregisterWatchFn();
});

19.解释$scope是什么?

20.指令是什么?

21.DDO (Directive Definition Object)是什么?

22. 什么是单例模式?在AngularJS中哪里使用到这种模式?

23. interceptor是什么?哪里使用的比较多?

24.在指令执行和转换之前,如何修改指令模板?

25.如何验证文本输入值?

26.在应用中,如何实现全局的异常处理?

27.通过按钮点击事件如何隐藏一个元素?

28.如何通过数据模型值的变化来触发更多的行为?

29.如何根据checkbox的状态来禁用按钮?