2017-01-10 59 views
0

我在Angularjs中使用多個transclude組件的奇怪行爲: 第一個slot模型中的更改在控制器中不可見。在Angularjs中使用多個transclude組件的奇怪行爲1.6.1

<div ng-app="myApp" ng-controller="testController"> 

<script type="text/ng-template" id="component-template.html"> 
<div style="color:red;" ng-transclude="heading"> 
</div> 
<div style="color:blue;" ng-transclude="body"> 
</div> 
</script> 

Example1 
<input ng-model="example1Model"/> 

<test-component> 
    <panel-heading> 
     Example2 
     <input ng-model="example2Model"/> 
    </panel-heading> 
    <panel-body> 
    Example1Result:{{example1Model}}<br/> 
    Example2Result:{{example2Model}} 
    </panel-body> 
</test-component> 
</div> 

<script> 
angular.module("myApp", []) 
.controller("testController", function ($scope, $location) { 

}) 
.component("testComponent", { 
    templateUrl: "component-template.html", 
    transclude: { 
     heading: "panelHeading", 
     body: "panelBody" 
    }, 
    controller: function ($scope, $element, $attrs) {   
     this.$doCheck = function() { 

      //do anything 
     } 
    } 
}); 
</script> 

現在,如果你嘗試測試它在此的jsfiddle:https://jsfiddle.net/Lpveophe/1/

爲什麼綁定模型example2Model沒有工作? 但是example1Model綁定模型正常工作。

回答

0

您需要在ng-model中使用.才能使其正常工作。爲了更好地理解的範圍,請通過這篇文章:https://github.com/angular/angular.js/wiki/Understanding-Scopes

要使其工作,與o.example2Model取代example2Model無處不在,改變你的controller到:

.controller("testController", function ($scope, $location) { 
    $scope.o = {}; 
    $scope.o.example2Model = ''; 
}) 
+0

它的工作原理!謝謝! –