search
HomeWeb Front-endJS Tutorial10 Angular interview questions that range from happy to sad

10 Angular interview questions that range from happy to sad

Aug 26, 2020 am 10:30 AM
angularInterview questions

10 Angular interview questions that range from happy to sad

Although there are only 10 questions, they cover all aspects of angular development, including basic knowledge points, problems encountered during the development process, and more open questions. To identify the interviewer's basic level and project experience, if you were interviewing a year ago, it would definitely be a transition from comedy to tragedy? (PS: The answer is for reference only~).

Related tutorial recommendations: "angular Tutorial"

1. ng-show/ng-hide and ng-if## The difference between #?

We all know that ng-show/ng-hide actually hides and displays through

display. And ng-if actually controls the addition and deletion of dom nodes. Therefore, if we load DOM nodes based on different conditions, then the performance of ng-if is better than ng-show.

2. Explain what

$rootScrope and and# The difference between ##$scope? In layman terms

$rootScrope

is the father of all $scope on the page.

10 Angular interview questions that range from happy to sadLet’s take a look at how to generate

$rootScope

and $scope. step1: Angular parses

ng-app

and creates $rootScope in memory. step2: Angular returns to continue parsing, finds the

{{}}

expression, and parses it into a variable. step3: Then the div with

ng-controller

will be parsed and pointed to a controller function. At this time, the controller function becomes a $scope object instance. 3. How does the expression

{{yourModel}}

work? It relies on the $interpolation service. After initializing the page html, it will find these expressions and mark them, so every time it encounters a

{{}}

, it will set a $watch. And $interpolation will return a function with context parameters. When the function is finally executed, the expression $parse is transferred to that scope. 4. What is the digest cycle in Angular?

In each digest cycle, angular will always compare the value of the model on the scope. Generally, the digest cycle is automatically triggered. We can also use $apply to trigger manually. For deeper research, you can move to

The Digest Loop and apply

. 5. How to cancel

$timeout

, and stop a $watch()?To stop $timeout we can use cancel:

var customTimeout = $timeout(function () {
  // your code
}, 1000);

$timeout.cancel(customTimeout);

Stop a

$watch

:<pre class='brush:php;toolbar:false;'>// .$watch() 会返回一个停止注册的函数 function that we store to a variable var deregisterWatchFn = $rootScope.$watch(‘someGloballyAvailableProperty’, function (newVal) { if (newVal) { // we invoke that deregistration function, to disable the watch deregisterWatchFn(); ... } });</pre>6. How can I set the restrict in Angular Directive? What is the difference between @,=,& in scope?

restrict can be set separately:

  • A

    Matching attributes

  • E

    Match tag

  • C

    Match class

  • ##M
  • Match comment

    Of course you can set multiple values ​​such as
  • AEC
to perform multiple matches.

In the scope, @,=,& represent respectively when doing value binding

    @
  • Get a set string, it You can set it yourself or use {{yourModel}} for binding;

  • =
  • Two-way binding, binding some properties on the scope;

  • &
  • is used to execute some expressions on the parent scope. Commonly we set some functions that need to be executed

    angular.module(&#39;docsIsolationExample&#39;, [])
    .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
      $scope.alertName = function() {
          alert(&#39;directive scope &&#39;);
      }
    }])
    .directive(&#39;myCustomer&#39;, function() {
      return {
        restrict: &#39;E&#39;,
        scope: {
          clickHandle: &#39;&&#39;
        },
        template: &#39;<button ng-click="testClick()">Click Me</button>&#39;,
        controller: function($scope) {
          
          $scope.testClick = function() {
            $scope.clickHandle();
            
          }  
        }
      };
    });
    <div ng-app="docsIsolationExample">
    <div ng-controller="Controller">
      <my-customer click-handle="alertName()"></my-customer>
    </div>
     </div>

  • Codepen Demo: https://codepen.io/Jack_Pu/pen/NrpRBK

    Perform one-way binding.

    7. List at least three ways to implement communication between different modules?

Service
  • events, specify the bound event
  • Use $rootScope
  • Directly use
  • $parent
  • ,

    $$childHead, etc.

    directive to specify attributes between
  • ##directive data. Binding

8. What measures can be taken to improve Angular performance

  • Officially recommended, turn off debug,

    $compileProvider

  • myApp.config(function ($compileProvider) {
      $compileProvider.debugInfoEnabled(false);
    });
  • Use a binding expression that is {{::yourModel}}

  • Reduce the number of watchers

  • Avoid using ng-repeat in infinite scroll loading. For solutions, please refer to this

    article

  • 使用性能测试的小工具去挖掘你的angular性能问题,我们可以使用简单的console.time()也可以借助开发者工具以及Batarang

console.time("TimerName");
//your code
console.timeEnd("TimerName");

9. 你认为在Angular中使用jQuery好么?

这是一个开放性的问题,尽管网上会有很多这样的争论,但是普遍还是认为这并不是一个特别好的尝试。其实当我们学习Angular的时候,我们应该做到从0去接受angular的思想,数据绑定,使用angular自带的一些api,合理的路由组织和,写相关指令和服务等等。angular自带了很多api可以完全替代掉jquery中常用的api,我们可以使用angular.element$http,$timeout,ng-init等。

我们不妨再换个角度,如果业务需求,而对于一个新人(比较熟悉jQuery)的话,或许你引入jQuery可以让它在解决问题,比如使用插件上有更多的选择,当然这是通过影响代码组织来提高工作效率,随着对于angular理解的深入,在重构时会逐渐摒弃掉当初引入jquery时的一些代码。(?Po主就是这样的人,希望不要被嘲笑,业务却是赶着走)

所以我觉得两种框架说完全不能一起用肯定是错的,但是我们还是应该尽力去遵循angular的设计

10. 如何进行angular的单元测试

我们可以使用karam+jasmine 进行单元测试,我们通过ngMock引入angular app然后自行添加我们的测试用例。
一段简单的测试代码:

describe(&#39;calculator&#39;, function () {

  beforeEach(module(&#39;calculatorApp&#39;));

  var $controller;

  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe(&#39;sum&#39;, function () {
        it(&#39;1 + 1 should equal 2&#39;, function () {
            var $scope = {};
            var controller = $controller(&#39;CalculatorController&#39;, { $scope: $scope });
            $scope.x = 1;
            $scope.y = 2;
            $scope.sum();
            expect($scope.z).toBe(3);
        });    
    });

});

关于测试,大家可以看下使用karma进行angular测试

除了Karam , Angular.js团队推出了一款e2e(end-to-end)的测试框架protractor

参考

相关推荐:编程教学

The above is the detailed content of 10 Angular interview questions that range from happy to sad. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),