TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Steve Voltmer
NA
10
533
Set a property on an Angular model from the controller
Apr 24 2018 1:34 PM
I have some JSON that has two arrays of objects, one for "articles" and one for "allCategories". My controller is getting the data and assigning the two arrays to their own $scope as seen here:
$http.get(jsonFeed).success(
function
(response){
// JSON Data
$scope.articles = response.articles;
$scope.allCategories = response.allCategories;
Then I iterate over the allCategories array to populate my select in the view:
<div ng-app=
"newsArticleListing"
ng-controller=
"articleListing"
>
<!--<p>PATH: {{ path }} URL: {{ url }} SEARCH: {{ urlQuery }} LOCATIONOJB: {{ locationObj }}</p> -->
<form
class
=
"filter-form"
method=
"get"
name=
"Search"
target=
"_top"
>
<div
class
=
"filter-form-holder"
>
<div
class
=
"filter-form-item"
>
<span
class
=
"filter-form-label"
>Search by:</span>
</div>
<div
class
=
"filter-form-item"
>
<div
class
=
"filter-form-select-wrapper"
>
<select
class
=
"directorySelect angularFilter"
ng-model=
"catSelect"
ng-options=
"cat.name for cat in allCategories | orderBy: 'name'"
ng-change=
"changeHandler()"
style=
"height:59px; width:385px; border-radius:0px;"
>
<option value=
''
> All News</option>
</select>
</div>
</div>
<input style=
"border-radius: 5px;"
class
=
"text-search"
ng-model=
"courseTextSearch"
type=
"text"
name=
"search"
placeholder=
"Search all listings"
/>
</div>
</form>
Then I iterate over the "articles" array and filter based off of the "catSelect" model, which is an object that contains { "name" : "somevalue", "filterValue" : "somevalue"} I am using "catSelect.filterValue"
<div
class
=
"container"
>
<ul
class
=
"news-list"
>
<li
class
=
"news-list-item"
dir-paginate=
"article in articles | orderBy: '-date' | filter : { categories : catSelect.filterValue || undefined } | filter: courseTextSearch | itemsPerPage:pageSize"
current-page=
"pagination.current"
>
<article
class
=
"news-block"
>
<div ng-show=
"catChecker(catSelect.filterValue, article.categories)"
class
=
"news-block-holder is-blue"
>
<strong
class
=
"news-block-taxonomy"
>{{ catSelect.name }}</strong>
</div>
<a
class
=
"news-block-img-link"
href=
"{{ article.link }}"
target=
"_blank"
>
<div
class
=
"news-block-img"
style=
"background-image: url('{{ article.thumbImage }}'); background-size: cover;"
>
<img ng-src=
"{{ article.thumbImage }}"
alt=
"{{ article.alt }}"
/>
</div>
</a>
<div
class
=
"news-block-body"
>
<a
class
=
"news-block-link"
ng-show=
"article.title"
href=
"{{ article.link }}"
target=
"_blank"
>{{ article.title }}</a>
</div>
</article>
</li>
</ul>
</div>
<!-- Paging -->
<div
class
=
"pagePagination"
>
<dir-pagination-controls max-size=
"8"
direction-links=
"true"
>
</dir-pagination-controls>
</div>
Everything works fine, accept when I try to add in the ability to set the selected value from a query string. I can't assign the property "catSelect.filterValue" from within my controller as below: (fyi.. I have predefined some links currently that are in the structure "?
year=someyear&topics=sometopic")
if
(window.location.href.includes(
'?'
))
{
var
queryString = window.location.href.split(
'?'
);
var
values = queryString[1].split(
'&'
);
var
topics = values[1].split(
'='
);
var
topic = topics[1];
console.log(topic);
if
(topics[0] ==
"topics"
){
$scope.catSelect.filterValue = topics[1];
console.log($scope.catSelect.filterValue);
}
}
I thought because the model "catSelect" was two-way bound to the view I would have access to set the property of "$scope.catSelect.filterValue". What am I missing?
Reply
Answers (
1
)
Uploading two files by two file input in Angular JS
anchor ng-click inside updatepanel