angular.js - Mengapakah pendakap kerinting berganda dalam ng-repeat tidak boleh diambil semasa penyusunan dinamik?
仅有的幸福
仅有的幸福 2017-05-15 16:53:41
0
1
632

Templat disusun dalam arahan, dan skop ng-repeat mempunyai data item , tetapi kurungan kerinting berganda tidak boleh mengakses , dan skop ng-repeat The $ ibu bapa semuanya adalah skop arahan, tetapi interpolasi pendakap kerinting gagal. Saya tidak tahu sebabnya

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="//cdn.bootcss.com/angular.js/1.3.13/angular.js"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body ng-app='app' ng-controller='app'>
    <compile-scope tpl="
    <span>我是bind---</span><p ng-bind='data.somevalue'></p>
    <span>我是括号---</span><p>{{data.somevalue}}</p>
    <ul ng-repeat='item in data.list' ng-click='checkscope(this)'>
    <li><span>我是bind---</span><span ng-bind='item.value'></span>,<span ng-bind='item.val'></span></li>
    <li><span>我是括号---</span>{{item.value}},{{item.val}}</li>
    </ul>
    " twowaybinding="{data:data,checkscope:checkscope}"></compile-scope>
</body>
</html>
<script>
angular.module('app',[])
.controller('app',function($scope){
    console.log('i am scope from ctrler',$scope);
    $scope.checkscope = function(scope){
        console.log(scope)
    }
 $scope.data = {
    somevalue : 'somevalue',
    list:{
    a:{value:'i am value1 from list',val:'i am val1 from list'},
    b:{value:'i am value2 from list',val:'i am val2 from list'},
    }
 }
})
.directive('compileScope',function($timeout, $compile){
    return{
        restrict:'E',
        scope:{
            tpl:'@',
            twowaybinding:'='
        },
        template:'',
        link:function(scope, ele, attr){
            angular.forEach(scope.twowaybinding, function(value, key){
                scope[key] = value;
                $timeout(function(){
                    scope.$watch(function(){
                        return scope[key];
                    },function(){
                        scope.$parent[key] = scope[key]
                    },true)
                    scope.$parent.$watch(function(){
                        return scope.$parent[key];
                    },function(v){
                        scope[key] = scope.$parent[key];
                    },true)
                })
            });
             $timeout(function(){
                var cpl = $compile(scope.tpl)(scope);
                 ele.html('');
                 ele.append(cpl)
             })
        }
    }
})
</script>
仅有的幸福
仅有的幸福

membalas semua(1)
滿天的星座

Nampaknya pendakap kerinting tpl dalam {{}} telah dipadamkan oleh angular atas sebab yang tidak diketahui! ! Alangkah peliknya! !

Pemahaman saya tentang

ialah jika ia diproses sebagai @, jika rentetan mengandungi {{}}, maka ini sendiri adalah ungkapan, jadi angular akan berada dalam controller semasa $scope Lihat untuk pembolehubah item.value dan item.val dan menggantikan kandungan dalam {{}} Hasil selepas penggantian ialah directive yang anda perolehi scope.tpl, jadi secara semulajadi tiada kandungan seperti {{ item.value }} di dalamnya.

Bagaimana jika anda mencuba cara lain:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="//cdn.bootcss.com/angular.js/1.3.13/angular.js"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body ng-app='app' ng-controller='app'>
    <compile-scope tpl="'<span>我是bind---</span><p ng-bind=\'data.somevalue\'></p><span>我是括号---</span><p>{{data.somevalue}}</p><ul ng-repeat=\'item in data.list\' ng-click=\'checkscope(this)\'><li><span>我是bind---</span><span ng-bind=\'item.value\'></span>,<span ng-bind=\'item.val\'></span></li><li><span>我是括号---</span>{{item.value}},{{item.val}}</li></ul>'" twowaybinding="{data:data,checkscope:checkscope}"></compile-scope>
</body>
</html>


<script>
angular.module('app',[])
.controller('app',function($scope){
    console.log('i am scope from ctrler',$scope);
    $scope.checkscope = function(scope){
        console.log(scope)
    }
 $scope.data = {
    somevalue : 'somevalue',
    list:{
    a:{value:'i am value1 from list',val:'i am val1 from list'},
    b:{value:'i am value2 from list',val:'i am val2 from list'},
    }
 }
})
.directive('compileScope',function($timeout, $compile){
    return{
        restrict:'E',
        scope:{
            tpl:'=',
            twowaybinding:'='
        },
        template:'',
        link:function(scope, ele, attr){
            angular.forEach(scope.twowaybinding, function(value, key){
                scope[key] = value;
                $timeout(function(){
                    scope.$watch(function(){
                        return scope[key];
                    },function(){
                        scope.$parent[key] = scope[key]
                    },true)
                    scope.$parent.$watch(function(){
                        return scope.$parent[key];
                    },function(v){
                        scope[key] = scope.$parent[key];
                    },true)
                })
            });
             $timeout(function(){
                 console.log(scope.tpl);
                var cpl = $compile(scope.tpl)(scope);
                 ele.html('');
                 ele.append(cpl)
             })
        }
    }
})
</script>



Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan