Answer the question
In order to leave comments, you need to log in
Angular. How to link directive and controller?
Hello! I'm writing a simple application and I'm already tired of sorting out the errors that engular writes to me ... They are incomprehensible to me ... The bottom line is - I want to connect the controller so that it works with the directive. ALL! I read the docs, questions on SO, a bunch of tutorials... I can't understand what is required of me...
Here is the code
(function(){
var app = angular.module('rooms', ['rooms.controllers']);
})();
(function() {
var app = angular.module('rooms.controllers', []);
app.factory('socket', function ($rootScope) {
var socket = io('http://localhost:9999/');
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
});
app.controller('UsersController', function($scope, socket) {
$scope.leaders = [{
leader: 'Test Leader',
}];
$scope.click = function() {
alert($scope);
}
socket.emit('user online');
socket.on('user online', function(leaders) {
console.dir(leaders);
$scope.leaders = leaders;
});
})
.directive('usersList', function(){
return {
restrict: 'EA',
templateUrl: 'js/templates/users-list.html'
};
});
app.controller('ChatsController', function($scope, socket) {
$scope.messages = [];
$scope.send = function(msg){
console.dir(msg);
socket.emit('common chat message', msg || {});
};
})
.directive('usersChats', function(){
return {
restrict: 'EA',
templateUrl: 'js/templates/users-chats.html'
};
});
})();
<div id="users">
<h3 ng-click="click()">users</h3>
<ul id="userList" class="list-group">
<li class="list-group-item" ng-repeat="u in leaders" ng-click="alert(u.leader)">
{{u.leader}}
</li>
</ul>
</div>
<users-list></users-list>
click is undefined
send is undefined
<div ng-controller="UsersController" users-list></users-list>
Error: [$injector:unpr] errors.angularjs.org/1.4.4/$injector/unpr?p0=eProv...
var gulp = require('gulp'),
csso = require('gulp-csso'),
concat = require('gulp-concat'),
minify = require('gulp-minify');
gulp.task('default', function() {
gulp.src([
"pub/js/app.js",
"pub/js/controllers/controllers.js"
])
.pipe(concat('rooms-app.js'))
.pipe(minify())
.pipe(gulp.dest('pub/'));
gulp.src([
"pub/css/*.css"
])
.pipe(concat('rooms-app.css'))
.pipe(csso())
.pipe(gulp.dest('pub/'));
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question