I have a need for inheriting scope from a parent controller in a directive. I don't necessarily want to leave scope: false. I also don't necessarily want to use an isolated scope, because it requires a lot of work to get the values I do care about linked properly (think lots of values in a parent controller).
Does it make sense to use scope:true
in my directive if I want to update parent scope?
<div ng-controller="MyCtrl">
Hello, {{name}}!
<my-directive></my-directive>
</div>
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.name = 'Dave';
}
myApp.directive('myDirective', function() {
return {
scope: true,
restrict: 'EA',
link: function(scope, elem, attrs) {
scope.updateName = function(newName) {
console.log('newName is: ' + newName);
scope.name = newName;
}
},
template: '<input ng-model="updatedName" placeholder="new name value"> <button ng-click="updateName(updatedName)">Update</button>'
}
})
Please check out the fiddle
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…