Angular.js: ng-ifμ—μ„œ μ—¬λŸ¬ 번 λ°”μΈλ”©λœ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λ©΄ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

에 λ§Œλ“  2015λ…„ 03μ›” 22일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: angular/angular.js

λ‚΄ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

one_time.js

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

sample.controller('OneTimeController', function () {
    this.name = 'John';
    this.blah = 'sdflk';
});

index.html
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script type="text/javascript" src="one_time.js"></script>
</head>

<body ng-app="sample">
<div ng-controller="OneTimeController as test">
  <p>Input something in the input box:</p>
  <p>Name: <input type="text" ng-model="test.name" value="John"></p>
  <p ng-bind="test.name"></p>
  <p ng-bind="::test.name"></p>
  <p ng-if="test.name && test.blah">testing</p> <!-- works perfectly-->
  <p ng-if="::test.name && test.blah">testing with first expression as one time binded</p> <!-- works but the second property is not one time binded-->
  <p ng-if="::test.name && ::test.blah">testing with one time bind</p><!-- doesn't work throws syntax error-->

</div>
</body>
</html>

name 속성을 ν•œ 번 λ°”μΈλ”©ν•˜λ €λŠ” ng-if 쑰건이 μžˆλŠ” <p> νƒœκ·Έλ₯Ό ν‘œμ‹œν•˜λ €κ³  ν•©λ‹ˆλ‹€. λ‚˜λŠ” 그것이 λ³€ν•˜μ§€ μ•Šμ„ κ²ƒμ΄λΌλŠ” 것을 이미 μ•Œκ³  μžˆμœΌλ―€λ‘œ ng-ifμ—μ„œλ„ ν•œ 번 바인딩을 μ‹œλ„ν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. ν•œ 번 바인딩을 μ‚¬μš©ν•˜μ§€ μ•Šκ±°λ‚˜ 이 경우 <p ng-if="::test.name && test.blah">testing with first expression as one time binded</p> 첫 번째 ν‘œν˜„μ‹μœΌλ‘œ ν•œ 번만 바인딩을 μ‚¬μš©ν•  λ•Œ μ™„λ²½ν•˜κ²Œ μž‘λ™ν•˜μ§€λ§Œ λ‹€μŒκ³Ό κ²°ν•©λœ λ‘˜ μ΄μƒμ˜ ν‘œν˜„μ‹μ—μ„œ ν•œ 번 바인딩을 μ‚¬μš©ν•˜λ €κ³  ν•  λ•Œ κ°€λ“œ μ—°μ‚°μžλŠ” ꡬ문 였λ₯˜λ‘œ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.

Error: [$parse:syntax] Syntax Error: Token ':' not a primary expression at column 14 of the expression [test.name && ::test.blah && ::test.blah] starting at [::test.blah && ::test.blah].
http://errors.angularjs.org/1.3.14/$parse/syntax? 

이것은 λ²„κ·Έμž…λ‹ˆκΉŒ μ•„λ‹ˆλ©΄ μ‹€μ œ ꡬ문 였λ₯˜μž…λ‹ˆκΉŒ? angularjs 버전 1.3.14λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

예: <p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p> ν‘œν˜„μ‹μ΄ μžˆλŠ” μ •μ‹œ λ°”μΈλ”©μ—λŠ” () λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

λͺ¨λ“  3 λŒ“κΈ€

예: <p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p> ν‘œν˜„μ‹μ΄ μžˆλŠ” μ •μ‹œ λ°”μΈλ”©μ—λŠ” () λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

@piernik κ°μ‚¬λŠ” 맀λ ₯처럼 μž‘λ™ν•©λ‹ˆλ‹€. (::test.name) && (::test.blah) μ‹œλ„ν–ˆμ§€λ§Œ 첫 번째 :: κ°€ ν•œ 번 바인딩을 ν™œμ„±ν™”ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. :+1:

μΌνšŒμ„± 바인딩(::)은 ng-if λ‚΄μ—μ„œ μ‚¬μš©λ˜λŠ” λͺ¨λ“  ν‘œν˜„μ‹μ— μ μš©λ©λ‹ˆλ‹€.
<p ng-if="::test.name && test.blah"> is same as <p ng-if="::(test.name && test.blah)">

λΆˆν–‰νžˆλ„ μš°λ¦¬λŠ” Angular js의 단일 ν‘œν˜„μ‹μ—μ„œ μΌνšŒμ„± 바인딩과 단방ν–₯ 바인딩 ν‘œν˜„μ‹μ„ ν˜Όν•©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰