, , , API, , ( , , )
var app = angular.module('MyApp', []);
app.service('myService', function($http){
this.saveDataToServer = function(str){
return $http.post('/url/here/', {str: str});
};
});
app.directive('myDirective', function(){
return {
restrict: 'ea',
scope: {
},
templateUrl: '/my-template.html',
controller: function(myService){
$scope.onAction = function(){
myService.saveDataToServer($scope.message).
then(function(res){
});
};
}
};
});
<div>
<input type="text" ng-model="message">
<button ng-click="onAction()">Save</button>
</div>
<my-directive></my-directive>
:
app.controller('MyOtherController', function(myService){
$scope.onSomeAction = function(str){
myService.saveDataToServer(str).
then(function(res){
});
};
});
<div ng-controller="MyOtherController">
<button ng-click="onSomeAction('hello world')">Hello</button>
</div>
, , . , . $controller , , , :
app.controller('MyController', function($scope){
$scope.colors = [
{color: 'Red'},
{color: 'Green'},
{color: 'Blue'}
];
});
app.directive('MyDirective', function(){
return {
scope: {
colors: '='
},
templateUrl: '/my-template.html'
};
});
<div>
<select ng-options="opt.color for opt in colors">
</div>
<my-directive colors="colors"></my-directive>
, , , , , , .
, , , . .