programing

$parse, $interpolate 및 $compile 서비스의 차이점은 무엇입니까?

sourcejob 2023. 2. 8. 17:51
반응형

$parse, $interpolate 및 $compile 서비스의 차이점은 무엇입니까?

와의 차이는 무엇입니까?$parse,$interpolate그리고.$compile서비스를 제공합니까?저에겐 모두 같은 작업을 합니다.템플릿을 가져와 템플릿 함수로 컴파일합니다.

이것들은 모두 Angular에 도움이 되는 서비스의 예입니다.JS 뷰 렌더링(단,$parse그리고.$interpolate이 도메인 외부에서 사용할 수 있습니다).각 서비스의 역할을 설명하기 위해 이 HTML의 예를 들어 보겠습니다.

var imgHtml = '<img ng-src="/path/{{name}}.{{extension}}">'

및 스코프의 값:

$scope.name = 'image';
$scope.extension = 'jpg';

이 마크업을 보면, 각 서비스가 표로 가져오는 것은 다음과 같습니다.

  • $compile- 마크업 전체를 링크 함수로 만들 수 있습니다.이 함수는 특정 스코프에 대해 실행되었을 때 HTML 텍스트 조각을 모든 지시와 함께 역동적인 라이브 DOM으로 변환합니다(여기서:ng-src모델 변경에 대응합니다.$compile(imgHtml)($scope)과 같이 호출하면 결과적으로 모든 DOM 이벤트 경계가 포함된 DOM 요소를 얻을 수 있습니다. $compile을 이용하고 있다$interpolate(다른 것들 중에서도) 그 일을 하기 위해서.
  • $interpolate는 삽입 표현식이 포함된 문자열을 처리하는 방법을 알고 있습니다.다음은 예를 제시하겠습니다. /path/{{name}}.{{extension}}즉, 보간식을 포함한 문자열과 스코프를 사용하여 텍스트로 변환할 수 있습니다.생각할 수 있는 것은$interpolation매우 단순한 문자열 기반 템플릿 언어로서 서비스를 제공합니다.위의 예에서는 이 서비스를 다음과 같이 사용합니다.$interpolate("/path/{{name}}.{{extension}}")($scope)을 손에 넣다path/image.jpg스트링이 됩니다.
  • $parse에 의해 사용됩니다.$interpolate개별 표현을 평가한다(name,extension)를 스코프에 비교합니다.지정된 식에 대한 값을 읽고 설정하는 데 모두 사용할 수 있습니다.예를 들어 다음과 같이 평가합니다.name할 수 있는 표현:$parse('name')($scope)"이미지" 값을 얻습니다.값을 설정하려면 다음 작업을 수행합니다.$parse('name').assign($scope, 'image2')

이러한 모든 서비스가 함께 작동하여 AngularJS에서 라이브 UI를 제공합니다.단, 다른 레벨로 동작합니다.

  • $parse는 개개의 표현에만 관계합니다(name,extension)는 읽기/쓰기 서비스입니다.
  • $interpolate는 읽기 전용이며 여러 식을 포함하는 문자열에 관련되어 있습니다(/path/{{name}}.{{extension}})
  • $compileAngular의 중심에 있다.JS는 HTML 문자열(명령어와 보간식 포함)을 라이브 DOM으로 변환할 수 있습니다.
$interpolate-->

Let us say you have two variables assigned to $scope object in the controller,

$scope.a = 2;
$scope.b = 3;

var f = $interpolate("Result is : {{a*b}}");
var result = f($scope);
console.log(result);   --->'Result is 6'

This means that $interpolate can take the string which has one or more angular expressions.

Now $parse:

var f1 = $parse("a*b");
var result1 = f1($scope);
console.log(result1);   ----> '6'

**So $interpolate has the capability to evaluate a string with angular expressions mixed up in the string against the scope to get the result**.


Another important difference between $interpolate and $parse,$eval is:

**$interpolate has the capability to work with strings containing filters along with angular expressions.**

This feature is not accessible in $eval , $parse.

console.log($interpolate("Result is : {{a*b | currency : 'USD$'}}")($scope));

---> 'Result is USD $6.00'

$val 및 $val과 같은 $val 변수에 대한 쓰기 액세스 권한이 $valate에 없습니다.

$valuate, $valuate ---> 컨트롤러 또는 컨트롤러 사용처에 $valuate를 삽입할 필요는 없습니다.

$deval, $devalate는 어떤 컨텍스트에서도 평가할 수 있는 함수를 제공하지만 $eval은 항상 $deval에 대해 평가됩니다.

무대 뒤에서 $eval 및 $valentate는 $120만을 사용합니다.

여기 귀여운 설명이 있습니다.

var img = img/{{name}}.{{extension}}

$parse - > (name,extension) = > vimal , .jpg
$interpolate -> use angular $parse and convert into string -> img/vimal.jpg
$compile -> can turn html string img/vimal.jpg into live DOM so new img tag will be added inside our DOM with all prototype that a img tag have.

언급URL : https://stackoverflow.com/questions/17900588/what-is-the-difference-between-the-parse-interpolate-and-compile-services

반응형