How to $compile the value when using ng-repeat? - angularjs

<ul class="alert alert-link alert-info"
data-ng-show="messages">
<li data-ng-repeat="msg in messages" data-ng-bind-html="msg | unsafe"></li>
</ul>
The controller code looks as below:
...
var msg = 'You\'re logged in. The registration must ' +
+ 'be <a href="#" ' +
+ 'data-ng-click="logout(\'/ajax/logout/\', $event)">logout</a> of your profile.';
$scope.messages = [msg];
...
How to $compile a msg, if msg comes from the server?

In general it's a really really terrible idea to compile things in angular directly from your backend. You can usually tell that if you're written the word unsafe in your code, you are putting yourself at risk. Not to mention it's going to be hard to find issues with the code if the code is on some other server/model/database somewhere. You should just return a model you can use from your server.
If you must do this, you'll probably want to create a directive which has something like element.html($compile(msg)(scope)) in your link function.

Related

dir-pagination-controls only showing first 10 items. Angularjs

<tr dir-paginate="plan in access |itemsPerPage: 10" total-items="planCount" current-page="current">
<td>{{plan._id}}</td>
<td>{{plan.name}}</td>
<td>{{plan.email}}</td>
<td>{{plan.contactNumber}}</td>
<td>{{plan.accessToken}}</td>
<td>{{plan.downloadSpeed | toMBPS}}</td>
<td>{{plan.uploadSpeed | toMBPS}}</td>
<td>{{plan.issueTime}}</td>
and this is the directive
<div class="text-center">
<dir-pagination-controls auto-hide="false" on-page-change="pageChanged(newPageNumber)"></dir-pagination-controls>
</div>
and this is the angular code
$scope.planCount = 0;
$scope.current = 1;
$scope.pageChanged = function(value) {
console.log("Going to page" + value);
}
The data is coming from the server, but that is working fine. I have logged the data, and I am receiving the complete data. Also, it is the registering the changed page number correctly.
Still, it only shows the first ten items, doesn't matter on what page number I am.
Note: Don't mind the incomplete html. That is the only relevant part to my situation. I can't post the whole code.
Also, I am using this in a different page, but it's working fine in it.
<tr dir-paginate="plan in access |itemsPerPage: 10" total-items="planCount" current-page="current">
From above line remove total-items="planCount". It'll work here too.

st_url is not working properly

I am using sharethis in my application.
here is my code
<a style="transition: none;" ng-init="initShare()" class="st_sharethis_custom" ng-if="loggedIn"><i class="fa fa-share-alt" style="/*font-size: 29px;*/"></i><em>SHARE</em></a>
this code works (it gives me the current url and shares)
but now i want to dynamic url so i am adding st_url for my code.
here is the code
<a style="transition: none;" ng-init="initShare()" class="st_sharethis_custom" st_url="#page/{{page.Id}}" ng-if="loggedIn"><i class="fa fa-share-alt" style="/*font-size: 29px;*/"></i><em>SHARE</em></a>
now the problem is the url is taking as "#page/{{page.Id}}" it is not replacing with the base url and with the pageId.
I even tried the full url with pageId (https:/..../page/{{page.Id}})
even this not taking the value of pageId.
can anyone help me with this thanks in advance

Valid json dont work with ng-repeat

My response from Java servlet via Angular, the request content is text/html
and I used data.split:
d = response.data.replace(/^\s+|\s+$/g, ''); // remove /r/n
data = d.split(" ");
for(var i =0 ; i<data.length; i++){
data[i] = '{' + data[i] + '}'; // add {} to each k.v
}
The result looks like:
["{key:myKey,value:true}", "{key:myKey,value:true}"....]
And my HTML
<ul>
<li ng-repeat="line in fixedDBArray">
{{line.key}} - {{line.value}}
</li>
</ul>
The anguler data-binding look like:
$scope.fixedDBArray = data //response.data
And {{fixedDBArray}} works fine but {{line.key}} and {{line.value}} do not work. I had checked http://jsonlint.com/ and the json is valid.
Anyone knows what is the problem?
If you still want to fix this as it stands now. you can use replace method and make the value valid JSON object. i made a sample implementation of this here
make sure that you use more efficient regular expression for adding additional quotes.just posting it for your reference without considering performance or complexity.

how do I take value from this $scope

so I have this code in my chatting application, its for send message to the partner.
$scope.messages = {
from : $scope.datauser['data']['_id'],
fromname : $scope.datauser['data']['nama'],
to : $scope.tmpuserid,
message : $scope.tmp['sendmessage'],
time : moment()
};
I want to add text-to-speech features in my application, the question is how I take the value from $scope.messages but just the message because if I just write $scope.messages, TTS will read all data from from until time
Not sure exactly what you want, but it would be something like:
<div ng-repeat="message in messages">
<p>{{message.message}}</p>
</div>
EDIT: This answer is for iterating through an array of messages.
If it were just one messages object it would be:
<p>{{messages.message}}</p>
Maybe I'm missing something in your question, but if all you want to extract just the message from the messages $scope object you would use JS object dot notation e.g.
var justMessage = $scope.messages.message;
// justMessage = $scope.tmp['send message'];
// OR parse an array
var myPartnerMessages = [];
angular.forEach($scope.messages, function(msg, key) {
this.push(msg.message);
}, myPartnerMessages);
You can get the value from $scope as follows:
View.html
<div ng repeat="item in messages">
<div>{{item.message}}</div>
</div>
You just call the property as $scope.messages.message. Or since you already had it on another scope variable, you can call it as $scope.tmp['sendmessage'].
If you are trying to access from the HTML side, you would use it as this:
<p>{{messages.message}}</p>
Not exactly sure if this is what you need from reading your question, though.
if it is a single object not an array if is an array you should use ng-repeat
On js end you can get it by
$scope.messages.message
On html end you can get it by
{{messages.message}}

AngularJs: $location.path not working correctly

I am having a weird issue with location.path(),as I don't get redirected to the page I want.
I have a link in my HTML file as follow:
<a title="{{woa.name}}" href="#/" ng-right-click="" href="javascript:void(0)" ng-click="goToDetailPage(woa.pk,'workofart')">
The corresponding controller has the following code:
appTreasure.controller("mySecondController", function($scope, $http, $location) {
$scope.goToDetailPage = function(pk, selectedDetailsPage) {
newLocation = selectedDetailsPage + '/' + pk;
console.log("NEW: " + newLocation);
$location.path(newLocation);
}
// some more unrelated code
The current URL is: <base>/#/beinspiredby and the printed newLocation is something like <base>/#/workofart/someexistingpk. So, I am building the new location with valid values.
Unfortunately, when I click on the link, I get redirected to <base>/#/.
If I manually go to <base>/#/workofart/someexistingpk I correctly land to the page I am looking for.
Furthermore, in a third controller, I have exactly the same code for goToDetailPage and it works.
Where am I missing something?
I think is it because you have an href attribute in your anchor element (actually you have two).
Try this
<a title="{{woa.name}}" ng-right-click="" ng-click="goToDetailPage(woa.pk,'workofart')">

Resources