我做了一个自定义的角度指令,像这样第二次:角定制指令有多个问题 - 不更换工作:真,不会触发
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
,更新函数会被触发,但每输入一次点击只会触发一次。第二次点击输入时,该功能不会被触发!
帮助!
建议您在[plunker](http://plnkr.co/edit/?p=catalogue)中进行工作演示。显示的一些代码在范围属性和html属性之间不匹配。更何况'alt'和'src'对于无线电来说也没有意义 – charlietfl