angular.js – angular scope
PHP中文网
PHP中文网 2017-05-15 16:58:35
0
3
547

I encountered some confusion when learning instructions today:

    <!DOCTYPE html>
<html ng-app="app">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="../css/lib/bootstrap4.css">

    <script type="text/javascript" src='../js/lib/angular.js' ></script>
    <script type="text/javascript" src='js/scope2.js'></script>
</head>

<body>
    
    <p ng-init = 'someProperty="some date"'>
    
        <p ng-init='siblingProperty="moredata"'>
            Inside p Two : {{aThirdProperty}}
    
            <p ng-init="aThirdProperty='data for 3rd property'" ng-controller="someCtrl">
                Inside p Three: {{aThirdProperty}}
    
                    <p ng-controller="secondCtrl">
                        Inside p Four: {{aThirdProperty}}
                        <br>
                        OutSide myDirective: {{myProperty}}
                        <p my-directive ng-init="myProperty='wow! that is cool'">
                            Inside myDirective : {{myProperty}}
                        </p>

                    </p>

            </p>

        </p>
    
    </p>
    
</body>
</html>

js

    var app = angular.module('app', []);
app.controller('someCtrl', ['$scope', function($scope){

    
}])

app.controller('secondCtrl', ['$scope', function($scope){
    console.log($scope.myProperty)    //undefined
    
}])
app.directive('myDirective',function(){
    return {
        restrict :'A',
        scope:false
        
    }

})

Why is undefined printed?

Then change the scope in the js command to scope:{}; Why does this pop up? In other words, why does the OutSide myDirective: wow! that is cool outside have value?

    Inside p Two :
    Inside p Three: data for 3rd property
    Inside p Four: data for 3rd property 
    OutSide myDirective: wow! that is cool
    Inside myDirective : wow! that is cool
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
过去多啦不再A梦
<p my-directive ng-init="myProperty='wow! that is cool'">
    Inside myDirective : {{myProperty}}
</p>

First of all, what you wrote has little to do with instructions. The scope of the instruction's scope是影响它的template is not here.

app.controller('secondCtrl', ['$scope', function($scope){
    console.log($scope.myProperty)    //undefined
}])

The reason for undefined here is because the controller is executed before ng-init. You can use delay or $watchmonitor

$timeout(function() {
  console.log($scope.myProperty)
});
// or
$scope.$watch('myProperty', function(newVal) {
  console.log($scope.myProperty)
})
阿神

Well,,, I understand the undefined above, but I am still a little confused about the latter one, that is to say, when scope:{}时,Inside myDirective : {{myProperty}}这个表达式还是属于外面那个作用域是不是?而此时template里面并没有设定,,,所以隔离作用域就没有起作用,我这样理解对吗?
还有一个问题,既然这样,,,那为什么如果把scope设定为scope:true is the following result:

    Inside p Two :
    Inside p Three: data for 3rd property
    Inside p Four: data for 3rd property 
    OutSide myDirective:
    Inside myDirect33ive : wow! that is cool

Can you give me some more advice? Thank you~~

Ty80

scope true is an independent and isolated scope
{}, only the properties inside {} will be isolated

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template