2015-11-03 12 views
0

我做了一个自定义的角度指令,像这样第二次:角定制指令有多个问题 - 不更换工作:真,不会触发

HTML - mydirective(data-datasource="mydirectivedata", data-updatefn="mydirectiveupdate")

控制器 -

$scope.mydirectivedata = //array of objects from API service call using $http 
$scope.mydirectiveupdate = function(a, b) {} 

指令 -

.directive('mydirective', function() { 
      return { 
       restrict: 'E', 
       replace: true, 
       scope: { 
        mydirectivedata: '=', 
        mydirectiveupdate: '&', 
       }, 
       controller: function($scope) { 
        $scope.updatefunction = function(id, type) { 
         $scope.mydirectiveupdate()(id, type); 
        }; 
       }, 
       templateUrl: '/partials/mydirective.html' 
      } 
     }) 

部分 -

<li data-ng-repeat="type in datasource.types"> 
    <input id="{{type.id}}" type="radio" name="{{$parent.datasource.id}}" src="" alt="{{$parent.datasource.id}}" aria-label="type.name" checked ng-value="true" data-ng-class="radio-selector" data-ng-model="type.isDefault" data-ng-change="updatefunction($parent.datasource.id,type)"> 
    <label for="{{type.id}}"><span style="background-image: url(undefined)"></span> 
    <h2 data-ng-bind="type.name">type.name</h2> 
    <p data-ng-bind="{{type.price}} | number :2" class="price">{{type.price}}</p> 
    <p data-ng-bind="type.description">type.description</p> 
    </label> 
</li> 

我的要求是:1。 当选择输入单选按钮,触发updatefunction,并反过来mydirectiveupdate函数传递所需的参数

的问题是:1。 如果我使用replace: true在我的指令中,$scope.updatefunction本身没有被触发 2.如果我不使用replace: true,更新函数会被触发,但每输入一次点击只会触发一次。第二次点击输入时,该功能不会被触发!

帮助!

+1

建议您在[plunker](http://plnkr.co/edit/?p=catalogue)中进行工作演示。显示的一些代码在范围属性和html属性之间不匹配。更何况'alt'和'src'对于无线电来说也没有意义 – charlietfl

回答

0

我不明白你的文章的第二行:

HTML - mydirective(data-datasource="mydirectivedata", data-updatefn="mydirectiveupdate")

我猜你的意思是这样:

<mydirective data-mydirectivedata="mydirectivedata" data-mydirectiveupdate="mydirectiveupdate" /> 

我也不知道什么是

部分 -

我假定这是您的指令模板。如果是这样,你不能使用replace: true,因为你没有父DOM元素。将整个 <li data-ng-repeat="type in datasource.types">...</li>代码放入<ul>标签内。

一旦你有数据源(这个HTML和部分),一旦这是mydirectivedata(指令定义)。查看您的示例,将所有这些考虑在内。