admin管理员组文章数量:1201992
I have a fiddle up here: /
What I'd like to do create a 'toggle' component, basically a custom checkbox but with html that changes if it is true or false, which is bound to a boolean in a controller.
When the user clicks on the toggle the model is updated the directive's view changes. It's similar to the examples at the end of the directives doc but the state would be bound so that it would be correct on startup.
var app = angular.module('App', []);
function Ctrl($scope) {
$scope.init = function() {
$scope.foo = true
}
}
app.directive('toggle', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
label: '@',
ngModel: '='
},
template:
'<div style="cursor: hand; cursor: pointer">{{label}}: {{ngModel}}</div>',
link: function(scope, element, attrs, controller) {
element.bind('click', function() {
scope.ngModel = false;
attrs.$set('ngModel', false);
console.log('plz', attrs.ngModel);
});
}
};
});
-
<div ng-app="App">
<div ng-controller="Ctrl" ng-init="init()">
<p>Foo in Ctrl: {{foo}}</p>
<toggle label="Foo" ng-model="foo"></toggle>
</div>
</div>
I have a fiddle up here: http://jsfiddle.net/KdkKE/44/
What I'd like to do create a 'toggle' component, basically a custom checkbox but with html that changes if it is true or false, which is bound to a boolean in a controller.
When the user clicks on the toggle the model is updated the directive's view changes. It's similar to the examples at the end of the directives doc http://docs.angularjs.org/guide/directive but the state would be bound so that it would be correct on startup.
var app = angular.module('App', []);
function Ctrl($scope) {
$scope.init = function() {
$scope.foo = true
}
}
app.directive('toggle', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
label: '@',
ngModel: '='
},
template:
'<div style="cursor: hand; cursor: pointer">{{label}}: {{ngModel}}</div>',
link: function(scope, element, attrs, controller) {
element.bind('click', function() {
scope.ngModel = false;
attrs.$set('ngModel', false);
console.log('plz', attrs.ngModel);
});
}
};
});
-
<div ng-app="App">
<div ng-controller="Ctrl" ng-init="init()">
<p>Foo in Ctrl: {{foo}}</p>
<toggle label="Foo" ng-model="foo"></toggle>
</div>
</div>
Share
Improve this question
edited Mar 6, 2017 at 10:31
Harry
5,7075 gold badges29 silver badges36 bronze badges
asked Mar 7, 2013 at 0:53
kreekkreek
8,8349 gold badges45 silver badges70 bronze badges
1 Answer
Reset to default 21I think you are just missing the use of $apply
. See it working here: http://jsfiddle.net/4TnkE/
element.bind('click', function() {
scope.$apply(function() {
scope.ngModel = !scope.ngModel;
});
});
It can also be used like this to avoid nesting in another function:
element.bind('click', function() {
scope.ngModel = !scope.ngModel;
scope.$apply();
});
本文标签: javascriptAngularUpdate model from directiveStack Overflow
版权声明:本文标题:javascript - Angular : Update model from directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738625371a2103433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论