Change root scope - angularjs

I have some issues with directive scopes...
wall.directive('onPaste', function() {
return{
restrict: 'A',
scope :true,
controller: function($scope, $http) {
$scope.getUrlInfos = function() {
$http({
method: 'POST',
data: {
firstPost: $scope.firstPost,
lastPost: $scope.lastPost
},
url: '/wall/json/parseUrl.json'
}).success(function(data){
$scope.firstPost = 99;
$scope.parseUrl = data; // response data
console.log($scope);
});
}
},
link: function (scope, element, attrs, parentCtrl) {
element.bind("paste", function(event) {
var element = this;
setTimeout(function () {
var text = $(element).val();
urls = findUrls(text);
scope.$apply(function(){
scope.firstPost = 10;
scope.getUrlInfos();
})
}, 100);
});
}
};
});
When I console.log($scope); the variable has all the scope... but as I understand it is a copy of the root scope. Any change to this scope doesn't appear on the screen. How can I return this scope to the root scope?

Assume that you have a defined root scope,
wall.run(function($rootScope){
$rootScope.firstPost = 1;
//.....
});
In AngularJS, $scopes prototypically inherit from their parent scope, all the way up to $rootScope. In JavaScript, primitive types are overwritten when a child changes them. So when you set $scope.firstPost in one of your controllers, the property on $rootScope was never touched, but rather a new visible property was added to the current scope.
So you need to pass the rootScope to directive controller,then change from there.
wall.directive('onPaste', function() {
return{
restrict: 'A',
scope :true,
controller: function($scope, $rootScope, $http) {
$scope.getUrlInfos = function() {
$http({
method: 'POST',
data: {
firstPost: $rootScope.firstPost,
lastPost: $rootScope.lastPost
},
url: '/wall/json/parseUrl.json'
}).success(function(data){
$rootScope.firstPost = 99;
$rootScope.parseUrl = data; // response data
console.log($rootScope);
});
}
},
link: function (scope, element, attrs, parentCtrl) {
element.bind("paste", function(event) {
var element = this;
setTimeout(function () {
var text = $(element).val();
urls = findUrls(text);
scope.$apply(function(){
scope.firstPost = 10;
scope.getUrlInfos();
})
}, 100);
});
}
};
});

Related

How to Pass parameter from AngularJS Directive to AngularJS controller function

Thanks in advance,Actually I want to call a function in controller from app.Directive, Please anyone let me know How I can call?Also I passing parameter to that function?I'm new in angular and here is all code.
var app = angular.module('quizApp', []);
app.controller("SaveCtrl", function (scope) {
$scope.Save = function (score) {
$scope.TestDetailsViewModel = {};
$scope.TestDetailsViewModel.CorrectAnswer = $scope.score;
$http({
method: "post",
url: "/Home/ResultSave",
datatype: "json",
data: JSON.stringify($scope.TestDetailsViewModel)
}).then(function (response) {
alert(response.data);
})
};})
app.directive('quiz', function (quizFactory) {
return {
restrict: 'AE',
scope: {},
templateUrl: '/Home/Dashboard',
link: function (scope, elem, attrs) {
scope.getQuestion = function () {
var q = quizFactory.getQuestion(scope.id);
if (q) {
scope.question = q.question;
scope.options = q.options;
scope.answer = q.answer;
scope.answerMode = true;
} else {
scope.quizOver = true;
//Calling function save(); in Controller
//scope.Save(scope.score);
}
};
}
}});
In the case of an isolated scope, the directive scope is completely unaware of its parent’s scope.
To call a function of a controller you have to bind that function to the scope of directive and then call scope functions from inside the directive.
For example:
app.controller('MainCtrl', function($scope) {
$scope.commonFunc = function(passed){
$scope.name = passed;
};
});
app.directive('demodirective', function(){
return {
scope: {
commonFunc: '&'
},
link: function(scope){
scope.commonFunc({passed:"world"});
}
};
});
HTML
<body ng-controller="MainCtrl">
<demodirective common-func="commonFunc(passed)">
</demodirective>
Hello {{name}}
</body>
For Reference - https://plnkr.co/edit/fMIsQ87jgdx49QSnWq4o?p=preview

Passing an object as attribute to compiled directive on the fly

I have a angular element on the page which needs to communicate with the rest of the non angular page elements.
I am creating directive elements on the fly, and appending it to its target div. I am trying to pass that created directive an object (ajax object), which contains just attributes.
The issue is that I can't figure out how to pass just this ajax object to the directive, as $compile requires a scope. When the http finishes, and because i have to use = in the directive, the directives are being over-ridden.
Please see my plunk: https://plnkr.co/edit/brTWgUWTotI44tECZXlQ ( sorry about the images ). Click the <button> to trigger the directive.
(function() {
'use strict';
var CHANNEL = 'podOverlay';
angular.module('CavernUI', [])
.controller('CavernCtrl', function($scope,getItemService) {
$scope.model = {};
var _pods = $scope.model.pods = {};
function getData(selector) {
$(selector).each(function(i, pod) {
_pods[+pod.dataset.item] = {
$: $(pod)
};
});
Object.keys($scope.model.pods).map(function(key) {
getItemService.getItem(key).success(function(response) {
_pods[key] = angular.extend(_pods[key], response);
$scope.$broadcast(CHANNEL, _pods[key], $scope);
});
})
}
$scope.runPodCheck = function(selector) {
getData(selector);
}
})
.directive('podchecker', function($compile) {
var createOverlay = function(e,data,scope){
scope.data = data;
// can i just pass data rather than scope.data?
// If I pass the scope, then when another $broadcast happens
// the scope updates, wiping out the last scope change.
// Scope here really needs to be a static object that's
// created purely for the hand off. But I don't know if
// that can be done.
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
}
return {
restrict: 'E',
scope: {
check: '&',
},
templateUrl: 'tpl.html',
link: function(scope,elm,attr){
scope.$on(CHANNEL,createOverlay);
}
};
})
.directive('overlay', function() {
return {
restrict: 'E',
scope: {
o: '=data' // here is the problem.
},
template: '<div class="overlay"><img ng-src="{{o.images.IT[0]}}"/></div>',
link: function(scope, elm, attr) {
}
}
})
.service('getItemService', ['$http', function($http) {
this.getItem = function(itemId) {
return $http({
method: 'GET',
url: 'https://www.aussiebum.com/ajaxproc/item',
params: {
id: itemId,
ajxop: 1
},
});
};
}]);
}());
Edits:
Expected ouput:
I'm not sure this is the best approach, but one way might be to manually create a new scope for each of the overlays.
So changed this:
var createOverlay = function(e,data,scope){
scope.data = data;
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
}
to this:
var createOverlay = function(e,data,scope){
var overlayScope = scope.$new(false); // use true here for isolate scope, false to inherit from parent
overlayScope.data = data;
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(overlayScope));
}
Updated Plnkr: https://plnkr.co/edit/wBQ1cqVKfSqwqf04SnPP
More info about $new()
Cheers!

How to update which template is shown based on what the user selects from the select box with a custom directive

I am trying to create a page that dynamically loads a template based on the option that the user chooses from a select box. I currently have it loading the template on page load but after that it does not change based on user action.
.directive('ngUsersearch', ['$compile', '$http', '$templateCache', function($compile, $http, $templateCache) {
var getTemplate = function(contentType) {
var templateLoader,
baseUrl = 'view2/components/',
templateMap = {
beer: 'beerList.html',
brewery: 'breweryList.html',
event: 'eventList.html',
guild: 'guildList.html'
};
var templateUrl = baseUrl + templateMap[contentType];
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;
}
var linker = function(scope, element, attrs) {
var loader = getTemplate(scope.ngModel);
var promise = loader.success(function(html) {
element.html(html);
}).then(function (response) {
element.replaceWith($compile(element.html())(scope));
});
}
return {
restrict:"E",
scope: {
ngModel: '='
},
link: linker
}
}]);
Here is my HTML:
<select ng-model="userFilter">
<option value="beer">Beer</option>
<option value="brewery">Brewery</option>
<option value="event">Event</option>
<option value="guild">Guild</option>
</select>
<ng-usersearch ng-model="userFilter"></ng-usersearch>
you forgot listen the change event of the model;
var linker = function(scope, element, attrs) {
scope.$watch('ngModel', function(newValue, oldValue) {
var loader = getTemplate(newValue);
var promise = loader.success(function(html) {
element.html(html);
}).then(function (response) {
element.replaceWith($compile(element.html())(scope)); // you compile and you have isolated scope?
});
});
}
on your compile the only scope available would be ngModel
This solution worked for me. I switched the way that the directive was loading the template. This can be done at the link function, but after the directive is set up and a part of the DOM, I was trying to remove the directive itself from the DOM by replacing it, which does not play well with how Angular's selectors work. So, now I am just replacing its contents. Also, in order to get the ng-repeat to work within the custom directive I had to add the search-results='searchResults' and then define that in the directives scope as well.
HTML:
<ng-usersearch ng-model="userFilter" search-results='searchResults'></ng-usersearch>
Controller:
.controller('View2Ctrl', [ '$scope', 'Restangular', function($scope, Restangular) {
$scope.userSearch = "";
$scope.userFilter = "beer";
$scope.search = function(userSearch, userFilter) {
$scope.searchResults = ("No " + userFilter + " Information Available");
Restangular.all('search?q=' + userSearch + '&type=' + userFilter + '&withBreweries=Y').customGET().then(function(data) {
$scope.searchResults = data;
});
};
}])
Directive:
.directive('ngUsersearch', ['$http', '$templateCache', '$compile', function($http, $templateCache, $compile) {
var getTemplate = function(contentType) {
var templateLoader,
baseUrl = 'view2/components/',
templateMap = {
all: 'all.html',
beer: 'beerList.html',
brewery: 'breweryList.html',
event: 'eventList.html',
guild: 'guildList.html'
};
var templateUrl = baseUrl + templateMap[contentType];
templateLoader = $http.get(templateUrl, {cache: $templateCache.get()});
return templateLoader;
}
var link = function(scope, element) {
scope.$watch('ngModel', function(newValue, oldValue) {
var loader = getTemplate(newValue);
var promise = loader.success(function(html) {
var rendered = $compile(html)(scope);
element.empty();
element.append(rendered); });
});
}
return {
restrict:"E",
scope: {
ngModel: '=',
searchResults: '='
},
link: link
}
}]);
I hope this helps other coders because I struggled with this for a day.

Testing an Angular directive with isolate scope and parent controller

I have a directive which creates an isolate scope and has a parent directive controller through the require option. In the link function I am adding some methods to the scope.
When I am trying to compile the directive in my test, I can't seem to get access to the methods I added in the link function, even though the link function is definitely executing.
The scope I have just seems to be an empty child scope of $rootScope. I have tried using element.isolateScope() but this just seems to give me the scope of the parent directive.
I am probably compiling something wrong, can someone help?
parent-directive.js
angular.module("app").directive("sortHead", function() {
return {
restrict: "A",
controller: function ($scope) {
$scope.sortField = undefined;
$scope.reverse = false;
this.setSortField = function(value) {
$scope.sortField = value;
};
this.setReverse = function(value) {
$scope.reverse = value;
};
this.getSortField = function(value) {
return $scope.sortField;
};
this.getReverse = function(value) {
return $scope.reverse;
};
}
};
});
directive-to-test.js
angular.module("app").directive("sortHeader", function() {
return {
restrict: "A",
templateUrl: "templates/sortHeader.html",
scope: {
title: "#",
sort: "#"
},
require: "^sortHead",
link: function(scope, element, attrs, controller) {
scope.sortBy = function(name) {
if (controller.getSortField() === name) {
controller.setReverse(!controller.getReverse());
} else {
controller.setSortField(name);
controller.setReverse(false);
}
};
scope.getSortField = function() {
return controller.getSortField();
};
scope.getReverse = function() {
return controller.getReverse();
};
}
};
});
test.js
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element("<th sort-header title='Name' sort='name'></th>");
$compile(element)(scope);
scope.$digest();
}));
The test doesn't seem to be workable in its current form.
Here is a plunker with fixed spec
beforeEach(module('app'));
beforeEach(inject(function ($rootScope, $compile, $templateCache, sortHeaderDirective) {
scope = $rootScope.$new();
$templateCache.put(sortHeaderDirective[0].templateUrl, '');
element = angular.element("<th sort-header title='Name' sort='name'></th>");
element.data('$sortHeadController', {});
$compile(element)(scope);
scope.$digest();
}));
it("should do something", inject(function () {
expect(element.isolateScope().title).toEqual('Name');
}));

Angularjs can't access parent scope function from directive

I cannot get the parent function call from the isolated scope..The purpose of this code is to create a widget directive which can be used multiple times on the same page... I tried some other option, but doesn't work either. It works using the parent scope.
What am I missing here.
var app = angular.module("winApp", []);
app.controller("winCtrl", function($scope, dataFactory) {
$scope.getData = function() {
dataFactory.get('accounts.json').then(
function(data) {
$scope.items = data;
});
};
});
app.directive("windowSmall", function() {
return {
restrict : 'EA',
replace : 'true',
scope : {
type : '&'
},
transclude: 'true',
templateUrl : 'windowtemplate.html',
link : function(scope, element, attrs) {
element.bind("load", function(){
console.log(attrs.type);
if (angular.equals(attrs.type, 'getData()')) {
scope.active = 'accounts';
console.log(attrs.type);
// scope.getData();
scope.$apply(function() {
scope.$eval(attrs.type);
});
}
});
}
};
});
app.factory('dataFactory', function($http) {
return {
get : function(url) {
return $http.get(url).then(function(resp) {
return resp.data;
});
}
};
});
HTML:
<div ng-app="winApp" ng-controller="winCtrl">
<window-small type = "getData()"> </window-small>
<br> <br>
<!--
<window-small type = "bulletin"> </window-small> -->
You can also use $rootScope for a full proof solution. Due to the fact that an application can have multiple parents but only one $rootScope.
https://docs.angularjs.org/api/ng/service/$rootScope
Replace your link function with :
link : function(scope, element, attrs) {
element.bind("load", function(){
console.log(attrs.type);
if (angular.equals(attrs.type, 'getData()')) {
scope.active = 'accounts';
console.log(attrs.type);
scope.type();
}
});
}
Fiddle : http://jsfiddle.net/X7Fjm/3/

Resources