AngularJS ng-bind-html not working in <ng-view> pages - angularjs

In the master page n-bind-html is working but when calling view pages any data didnt get .
ng-bind-html="home.title" is working in the index header div.
index.html
....
<title ng-bind-html="home.title"></title>
....
<ng-view></ng-view>
contact.html
but ng-view contact page ng-bind-html="home.title" not working and {{contact.info}} are not working.
<h4 ng-bind-html="home.title"></h4>
<p>{{contact.info}}</p>
controller:
$scope.trustedHtml = function (plainText) {
return $sce.trustAsHtml(plainText);
}
$http.get('admin/api/?s=getAbout&lang='+lang).success(
function(r){
$scope.about = r.text;
//$scope.about = r.email;
}
);
$http.get('admin/api/?s=getContact'+lang).success(
function(r){
$scope.contact = r;
//$scope.contacttext.infoH = r[0].infoH;
//$scope.contact.infoH = $sce.trustAsHtml(r.infoH);
}
);
$http.get('admin/api/?s=getHome&lang='+lang).success(
function(r){
$scope.home = r;
$scope.home.address = $sce.trustAsHtml(r.address);
$scope.home.phone = $sce.trustAsHtml(r.phone);
$scope.home.email = $sce.trustAsHtml(r.email);
$scope.home.title = $sce.trustAsHtml(r.title);
}
);
ng-view page of about is working .
about.html
<div class="container" style="font-size:16px;" ng-bind-html="trustedHtml(about)"></div>

You need to add below code code for dynamically added element. See Here . Hope this helps:
AngularApp.run(function($rootScope, $location, $timeout) {
$rootScope.$on('$viewContentLoaded', function() {
$timeout(function() {
componentHandler.upgradeAllRegistered();
});
});
});
How Component Handle works

Related

AngularJS - ng-click is calling multiple times

I'm new to AngularJS and have been learning it from W3Schools. My issue is that ng-click is not working properly. It's calling repeatedly. If I click on the element with ng-click once it calls the function once. If I click it again it calls the function twice. If I continue clicking the button the function is called as many times as I've clicked the element before.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1 id="h12" ng-click="changeName()" style="cursor:pointer;">{{firstname}}</h1>
</div>
<script>
var count = 0;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.changeName = function() {
var h12 = document.getElementById("h12").innerHTML;
alert(count);
if (count == 0) {
$scope.firstname = "Nelly";
changeNGClick(h12);
count++;
} else {
count++;
var x = "";
var div1 = document.getElementById("h12");
var align = div1.getAttribute("data-v");
alert(align);
$scope.firstname = align;
changeNGClick(h12);
}
}
return false;
});
function compile(element) {
var el = angular.element(element);
$scope = el.scope();
$injector = el.injector();
$injector.invoke(function($compile) {
$compile(el)($scope)
})
}
function changeNGClick(v) {
var el = document.getElementById("h12");
el.setAttribute("data-v", v);
compile(el);
}
</script>
<p>Click on the header to run the "changeName" function.</p>
<p>This example demonstrates how to use the controller to change model data.</p>
</body>
</html>
This is how the code will work. When I click the header which has the text John then it will change to display Nelly. If you click it again it should change back to John. The value will be saved on the <h1> tags data-v. I would also appreciate another way of doing this if you have any suggestions.

cordova plugin is not ready

I have a plugin to get information from DB, then display the information on the page. But I can't get it work when the page is loaded. It only works in a event handler.
I have tried to bootstrap my app in 'deviceready' event. Still don't work.
Please help me! Thanks!
The index.html file:
<body ng-controller='loginCtl'> {{company}}
<div class="list list-inset " style="margin-top:20px;">
<button class="button button-block button-calm" ng-click="login()">Login</button>
</div>
</body>
The app.js file:
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady(){
var body = document.getElementsByTagName("body")[0];
angular.bootstrap(body,['starter']);
//company =myplugin.getCompany(); -- doesn't work here
}
angular.module('starter', ['ionic','starter.controllers'])
.run(function($ionicPlatform) {
//......
});
angular.module('starter.controllers', [])
.controller('loginCtl', function($scope) {
//$scope.company = myplugin.getCompany(); --- doesn't work at here
$scope.login=function(){
company =myplugin.getCompany(); //work at here
$scope.company = company;
};
});
Thanks for comments. I finally resolved this issue by copying device plugin. My plugin.js is not correct.
This is my original code, which only works in an event handler.
var myplugin = {
getCompany:function() {
exec(function(info)
{company=info.company;},
null, "Myplugin", "getLoginInfo", []);
return company;
}
};
module.exports = myplugin;
This is copied from cordova-plugin-device, which works in "deviceready" event:
channel.createSticky('onCordovaMyReady');
// Tell cordova channel to wait on the CordovaInfoReady event
channel.waitForInitialization('onCordovaMyReady');
function Myplugin() {
this.company = 'none';
var me = this;
channel.onCordovaReady.subscribe(function() {
me.getLoginInfo(function(info) {
me.company = info.company;
channel.onCordovaMyReady.fire();
},
function(e) {
utils.alert("[ERROR] Error initializing Cordova: " + e);
});
});
}
Myplugin.prototype.getLoginInfo = function(successCallback, errorCallback) {
exec(successCallback, errorCallback, "Myplugin", "getLoginInfo", []);
};
module.exports = new Myplugin();
});

ng-animate not firing event (ng-enter)

I have the following angularjs application which loads content from Instagram, however, on page load I would like to fade the loaded content in with a smooth transition.
For some reason ng-animate doesn't seem to be firing the ng-enter event. so I can add a CSS animation. Is there something wrong?
HTML
<section ng-controller="ShowImages as images" class="page {{ loadedClass }}" >
<div ng-view>
......
</div>
JS
(function(){
//Place your own Instagram client_id below. Go to https://instagram.com/developer/clients/manage/ and register your app to get a client ID
var client_id = '83aaab0bddea42adb694b689ad169fb1';
//To get your user ID go to http://jelled.com/instagram/lookup-user-id and enter your Instagram user name to get your user ID
var user_id = '179735937';
var app = angular.module('instafeed', ['ngAnimate']);
app.filter('getFirstCommentFrom',function(){
return function(arr, user){
for(var i=0;i<arr.length;i++)
{
if(arr[i].from.username==user)
return arr[i].text;
}
return '';
}
})
app.factory("InstagramAPI", ['$http', function($http) {
return {
fetchPhotos: function(callback){
var endpoint = "https://api.instagram.com/v1/users/self/media/liked/";
endpoint += "?access_token=179735937.83aaab0.e44fe9abccb5415290bfc0765edd45ad";
endpoint += "&callback=JSON_CALLBACK";
$http.jsonp(endpoint).success(function(response){
callback(response.data);
});
}
}
}]);
app.controller('ShowImages', function($scope, InstagramAPI){
$scope.layout = 'grid';
$scope.data = {};
$scope.pics = [];
$scope.$on('$viewContentLoaded', function(){
$scope.loadedClass = 'page-feed';
});
InstagramAPI.fetchPhotos(function(data){
$scope.pics = data;
console.log(data)
});
});
})();
I also link to these in the HTML:
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular/angular-route.js"></script>
<script src="bower_components/angular/angular-animate.js"></script>
What am I doing wrong?
See here:
http://machinas.com/wip/machinas/instagramfeed/
Do u ve included the css file right??
I think u don't need this:
$scope.$on('$viewContentLoaded', function(){
$scope.loadedClass = 'page-feed';
});
The ng-enter is active when u set the value of the var that u use inside the ng-repeat
look at this http://codepen.io/shprink/pen/Ariyc

passing ng-show between two different controllers

I have a button which falls into Controller B and two block of HTML code which kind of falls under controller A...............and button falls into one block of HTML code
Example:
<div ng-controller="A">
<div ng-show="now">
<div>
<Button ng-controller="B"></Button>
</div>
</div>
<div ng-show="later">
</div>
</div>
On one button click I show up now block and later on button click of B controller I kind of hide now block and display later block.
How do I achieve this functionality?? I am not able to pass ng-show varibales between two different controller files......what should I use???
Hope this helps...!
angular.module('app', [])
.controller('A', function($scope) {
console.log('A');
$scope.state = {
now: true
};
$scope.showLater = function() {
$scope.state.later = true;
};
})
.controller('B', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="A" ng-app="app">
<div ng-show="state.now">
<div>
<button ng-controller="B" ng-click="showLater()">Show Later</button>
</div>
</div>
<div ng-show="state.later">LATER
</div>
<p> <pre ng-bind="state | json"></pre>
</p>
</div>
You could use a simple service that stores the state.
Example:
angular.module('mymodule').service('ActiveService', function() {
var service = {};
var status = false;
service.getStatus = function() {
return status;
}
service.toggle = function() {
status = !status;
}
return service;
})
And in your controller:
angular.module('mymodule').controller('SomeController', function(ActiveService) {
$scope.status = ActiveService.getStatus;
})
The Angularjs service is a singelton, so it will hold your values for you across different controllers, directives or pages.
Could also be used directly:
// Controller
$scope.service = ActiveService;
// Html
<div ng-show="service.getStatus()">
...
</div>
You can also achieve this by declaring the variable in $rootScope and watching it in controller A,
app.controller('A', function($scope, $rootScope) {
$rootScope.now = true;
$rootScope.later = false;
$rootScope.$watch("now", function() {
$scope.now = $rootScope.now;
$scope.later = !$rootScope.now;
})
});
In Controller B, you just change the value of now based on previous value like this on ng-click,
app.controller('B', function($scope, $rootScope) {
$scope.testBtn = function() {
$rootScope.now = !$rootScope.now;
}
});
I have implemented a button within different divs(now and later) in a plunker,
http://embed.plnkr.co/xtegii1vCqTxHO7sUNBU/preview
Hope this helps!

Trying to integrate Phonegap Connection API with AngularJS

I'm trying to use PhoneGap Connection API inside my Angular/Ionic project. What I want is to check the user's network and if it's NONE I want to show a specific div and if it's not NONE, show another div by using the ng-show directive.
Here is my code so far:
controller.js
.controller('LoginCtrl', function($scope, CheckConnection) {
$scope.net = CheckConnection.networkState();
})
.factory('cordovaReady', function() {
return function (fn) {
var queue = [];
var impl = function () {
queue.push(Array.prototype.slice.call(arguments));
};
document.addEventListener('deviceready', function () {
queue.forEach(function (args) {
fn.apply(this, args);
});
impl = fn;
}, false);
return function () {
return impl.apply(this, arguments);
};
};
})
.factory('CheckConnection', function(cordovaReady) {
return {
networkState: cordovaReady(function() {
var net = navigator.connection.type;
if(net == 'none') {
var internet = false;
} else {
var internet = true;
}
return internet;
})
};
})
view.html
<ion-nav-view name="login">
<div ng-hide="LoginCtrl.net">
NO CONNECTION {{net}}
</div>
<div ng-show="LoginCtrl.net">
CONNECTED {{net}}
</div>
</ion-nav-view>
I'm pretty new with Angular and PhoneGap so this issue may be very simple to solve but I just can't figure it out :( Any help will be much appreciated, thanks!
try this:
<ion-nav-view name="login" ng-controller="LoginCtrl">
<div ng-hide="net">
NO CONNECTION {{net}}
</div>
<div ng-show="net">
CONNECTED {{net}}
</div>
Ok, I found the solution! Angular has it's own way to check connection so I don't even need PhoneGap for this.
Here is my new controller.js:
.controller('LoginCtrl', function($window, $scope ) {
$scope.online = navigator.onLine;
$window.addEventListener("offline", function () {
$scope.$apply(function() {
$scope.online = false;
});
}, false);
$window.addEventListener("online", function () {
$scope.$apply(function() {
$scope.online = true;
});
}, false);
})
And view.html
<ion-nav-view name="login">
<div ng-hide="online">
NO CONNECTION
</div>
<div ng-show="online">
CONNECTED
</div>
</ion-nav-view>
Hope it helps someone else :D

Resources