How can I update the scope from a directive? Here's a sample code showing what I want.
html
<div ng-controller="sampleController">
<sample-directive></sample-directive>
{{message}}
</div>
js
var app = angular.module('sample')
.controller('sampleController',function($scope){
$scope.message = "Foo";
$scope.bar = function(){
$scope.message = "Bar";
}
}).directive("sampleDirective",function(){
return {
controller : "sampleController",
restrict : "E",
template : "<button type='button' ng-click='bar()'></button>"
}
});
When the button is clicked the $scope.message
is changed to bar but the view is not updated.(answered)
Here's another scenario:
html
<div ng-controller="sampleController as sc">
<sample-directive message="bar"></sample-directive>
{{sc.message}}
</div>
js
var app = angular.module('sample')
.controller('sampleController',function(){
var sc = this;
sc.message = "foo";
sc.bar = function(){
//change the sc.message to value that is passed to the message attribute in the sample directive
}
}).directive("sampleDirective",function(){
return {
restrict : "E",
template : "<button type='button' ng-click='sc.bar()'></button>",
scope : {
message : "@" //how can I pass this to the sc.message scope?
}
}
})