I beginning on this framework.
And I've a question about update a controller from another controller.
I know this question has many times asked.
But it's not like my problem.
I can resolve my problem with rootScope, but I don't like (Am I wrong ?)
This is my example:
http://jsfiddle.net/1Lwg20wz/
I thought it would update automatically with this part of code:
this.article = articleService.article;
I would like to update my PanelController when I click on "Read news".
How do you do ?
What is the best practice ?
First a couple of small issues in your example:
In the following HTML:
<a href="#" ng-click="selectArticle(article.index)" class="btn btn-default">
View news
</a>
Change href="#" to href="". Otherwise it will navigate to the root of your application instead of executing the ng-click.
In the same HTML change ng-click="selectArticle(article.index)" to ng-click="main.selectArticle(article.index)".
Now to the main issue.
In the PanelController you have the following code:
this.article = articleService.article;
This will copy the reference stored in articleService.article to this.article.
If articleService.article held a reference to the article object with id 1, this.article now also holds a reference to that object. They are however, two different references.
You have the following code to set the selected article object:
this.selectArticle = function(setArticle) {
articleService.article = articleService.articles[setArticle - 1];
};
Now articleService.article will hold a reference to another article object, for example with id 2. But, this.article in PanelController will still hold a reference to the article object with id 1.
The easiest solution to achieve what you want is to use the original articleService.article reference in your view, instead of creating a copy of it.
In PanelController
this.articleService = articleService;
In HTML:
<h1>{{panel.articleService.article.title}}</h1>
<p>{{panel.articleService.article.content}}</p>
Demo: http://jsfiddle.net/7ccop2hy/
Related
I have this element on my html page:
<div class="section-title" id="bladeSectionTitle"
ng-transclude="title">
</div>
I want to get the value displayed.
I have tried the following in my typescript page & only get null:
var title = document.getElementById("bladeSectionTitle").getAttribute('section-title');
The view source gives me this:
<dpn-blade-section is-checkbox-visible="true" is-checked="$component.showAll">
<section-title>
<h4>Show All</h4>
</section-title>
In this instance, the value I would be looking for is 'Show All'.
Which version of angular are you running? We really need more information here. Although accessing the DOM directly isn't the way to go with angular, you're looking at something like this
var title = document.querySelector('dpn-blade-section section-title h4').innerHTML;
I am confused on how to do a very, very trivial pattern with angular/firebase using angularfire.
Imagine you have a lil' old blog. You have some $scope.posts and you ng-repeat over them. Now you want to be able to click on one post and be routed to that post's page at '/posts/:id'. Since a $firebaseArray elements don't include their $id's how do you pass a unique id of a single post to the url and onto the postsCtrl?
<a ng-repeat="post in posts href="/posts/{{??????}}">{{post.title}}</a>
Update
The workaround I figured out is whenever I save a new post, I can save an attribute called "key" and then use this the way I would normally use the "id" attribute in every other framework.
$firebaseArray(postsRef).$add($scope.post).then(function(ref) {
ref.update({ key: ref.key()});
});
This hack, however, cannot be the solution to this simple, trivial, conventional pattern.
Thanks for any help.
You're instincts are right, that hack isn't the answer. Instead use angular's built in (key, value) option for ng-repeat:
<div ng-repeat="(id, post) in posts">
<a ng-href="/posts/{{post.id}}">{{post.title}}</a>
</div>
Additional reference material on how to use AngularFire with ng1 can be observed in this repo
I'm trying to prevent the value of a model from updating in a form.
For example :
I have a payment details form that lists the user's saved information (name, address etc) along with the form that is used to edit the same information.
I've been experimenting using the :: for one time binding as I don't want the displayed information to changed when input controls are changed (but I obviously want the models updated values so i can send them to the server for processing).
How do I update the displayed model values after the server responds that the changes have been saved, are ok etc? I can't seem to find a way to update the one time binding (as I'm guessing this is fully the intended functionality).
So I guess my question boils down to :
How do I selectively update bindings on some controls but not others?
Actually you just want to display different vars.
You should try with a temporary model object (that is a copy of your object like "editedObject") and when you validate you will update the original object.
See it working in this plunker
The editing space :
<input ng-model="editCopy.value"> <button ng-click="validateChange()">Change</button>
The ng-repeat :
<td ng-repeat="item in items" ng-click="editItem(item)">
{{item.value}}
</td>
The functions :
$scope.editItem = function(item){
$scope.editCopy = angular.copy(item);
$scope.editingItem = item;
}
$scope.validateChange = function(){
$http.get('index.html').success(function(){
$scope.editingItem.value = $scope.editCopy.value;
});
}
I think I have some sort of special code here as all I could google was "too simple" for my problem and it also didn't helped to come to a solution by myself, sadly.
I got a radio button group of 2 radios. I am iterating over "type" data from the backend to create the radio buttons.
My problem is the data binding: When I want to edit an object its "type" is set correctly, but not registered by the view so it doesn't select the desired option.
Follwing my situation:
Backend providing me this as "typeList":
[
{"text":"cool option","enumm":"COOL"},
{"text":"option maximus","enumm":"MAX"}
]
HTML Code:
<span ng-repeat="type in typeList track by type.enumm">
<input
type="radio"
name="type" required
ng-model="myCtrl.object.type"
ng-value="type">
{{type.text}}
</span>
Some Explanation
I don't want to use "naked" texts, I want to use some sort of identifier - in this case it is an enum. The chosen value shall be the entire "type", not only "type.text" as the backend expects type, and not a simple String.
So all I do with this is always a package thingy, the type.text is for like formatted/internationlized text etc.
A Pre-Selection works by setting this in the controller: this.object.type = typeList[0];
The first radio button is already selected, wonderful.
But why isn't it selected when editing the object. I made a "log" within the HTML with {{myCtrl.object.type}} and the result is {"text":"cool option","enumm":"COOL"}. The very same like when pre selecting. I already work with the same "technique" using select inputs, and it works fine. I also found some google results saying "use $parent because of parent/child scope". But 1) I didn't get that straight and 2) think it is not the problem here, as I use a controllers scope and not the $scope, or is this thinking wrong?
It might be explained badly, sorry if so, but I hope someone 1) get's what I want and 2) knows a solution for it.
Thank you!
If you're trying to bind to elements from an array, I believe you need to assign the actual elements from the array to your model property.
So this creates a new obj and sets it to $scope.selectedType (not what you want):
$scope.selectedType = {"text":"cool option","enumm":"COOL"};
whereas this assigns the first element of the array (which is what you want)
$scope.selectedType = $scope.typeList[0];
So to change the model, you can lookup the entry from the array and assign it to your model with something like this
$scope.selectedType = $scope.typeList.filter(...)
Here's a quick example of this approach http://plnkr.co/edit/wvq8yH7WIj7rH2SBI8qF
I know this is bad design but would like to introduce angular to a current project. I would like sayHello to be able to determine whether the element has the class 'is-a-favorite'
<div ng-click="sayHello(29, $event)" class="is-a-favorite" data-type="location" data-global-id="29" data-make-disappear="false"> </div>
$scope.sayHello=function(global_id,event){
//var selector=???
if(selector.hasClass('is-a-favorite')){
console.log("this is-a-favorite");
}
};
How would (or could) I get a reference to current DOM element to query via hasClass?
thx
The clicked element is available as $event.target, so you could check $($event.target).attr('class') or something similar.
EDIT: actually, what you'd want is to check $($event.target).hasClass('is-a-favorite')