ng-model
親htmlタグから変更するディレクティブを作成しようとしていますが、機能していません:
var app = angular.module('myApp',[]);
app.controller('ParentController',['$scope', function($scope) {
$scope.anyVar = "Anything";
$scope.list = ['It doesn\'t work','It also doesn\'t work'];
}]);
app.directive('customValue', [function() {
return {
restrict: 'A',
require: '^?ngModel',
link: function(scope, element, attr, ngModel) {
element.bind('click',function() {
var element = angular.element(this);
ngModel.$setViewValue(element.attr('custom-value'));
});
scope.$watch(function(){
return ngModel.$modelValue;
}, function(modelValue){
console.log("Wow, it was changed to : " + modelValue)
});
}
};
}]);
これは私の見解です:
<div ng-app="myApp">
<div ng-controller="ParentController">
{{anyVar}}
<ul ng-model="anyVar">
<li>
<a custom-value="111">It' not working</a>
</li>
<li>
<a custom-value="222">It's not working as well</a>
</li>
<li>
----------------------
</li>
<li ng-repeat="item in list">
<a custom-value="333">{{item}}</a>
</li>
</ul>
</div>
</div>
ng-repeat の有無にかかわらず、内部ディレクティブから親 ng-model を更新するにはどうすればよいですか。
フィドルを作成しました