ディレクティブで親コントローラーからスコープを継承する必要があります。必ずしもスコープを離れたくない: false。また、必要な値を適切にリンクするには多くの作業が必要になるため、必ずしも分離スコープを使用する必要はありません (親コントローラーの多くの値を考えてください)。
scope:true
親スコープを更新したい場合、ディレクティブで使用するのは理にかなっていますか?
<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>'
}
})
フィドルをチェックしてください