Calling template inside ngView does not support angular stackoverflow directive
My question is how to use AngularJS directives
in a template internally ngView
in an AngularJS app.
Define: The
app is single page, so it loads index.html containing a div element (url template) in the DOM with an ng-view attribute.
Main Page(
index.html ) :
<html data-ng-app="App" data-ng-controller="AppCtrl">
<head>
<title>Angular App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<!-- primary nav -->
<a href="#/page1">Page 1</a>
<a href="#/page2">Page 2</a>
<a href="#/page3">Page 3</a>
<!-- display the view -->
<div ng-view>
</div>
</body>
</html>
app.js:
angular.module('App', [])
.controller('AppCtrl', function($rootScope, appLoading) {
$rootScope.topScope = $rootScope;
$rootScope.$on('$routeChangeStart', function() {
appLoading.loading();
});
})
.config(function($routeProvider) {
$routeProvider.when('/page1', {
controller : 'Page1Ctrl',
templateUrl : 'page1.html'
})
.when('/page2', {
controller : 'Page2Ctrl',
templateUrl : 'page2.html'
})
.when('/page3', {
controller : 'Page3Ctrl',
templateUrl : 'page3.html'
})
.otherwise({
redirectTo: '/home'
});
})
page1.html:
<div class="form">
<form class="login-profile" method="post" action="" name="editfrm">
<input type="text" name="email" value="" id="txtemail" data-ng-model="email" required>
<input type="password" name="password" value="" id="txtpassword" data-ng-model="password" required>
<input type="button" value="Save" name="submit">
</form>
</div>
Problem:
Url template called internally ngView
is not supported AngularJS deirective
.
data-ng-model="email"
and data-ng-model="password"
don't work when called in ngView when a link is clicked<a href="#/page1">Page 1</a>
Any help would be appreciated. Thanks to
source to share
Without seeing any code for yours Page1Ctrl
, it's hard to tell, but it looks like you're trying to communicate between controllers using $rootScope
, no?
Okay, just don't. Use $routeParams
either a service for this . For example:
// app controller
.controller('AppCtrl', function(User) {
User.set({email:'email', password:'password'}); // set user
})
// page 1 controller
.controller('Page1Ctrl', function($scope, User) {
$scope.user = User.get(); // get user
})
// user service
.service('User', function() {
var user = null;
return {
get: function() {
return user;
},
set: function(val) {
user = val;
}
};
});
and the associated HTML
<input type="text"
name="email"
data-ng-model="user.email"
required>
<input type="password"
name="password"
data-ng-model="user.password"
required>
source to share
When you click <a href="#/page1">Page 1</a>
, it loads Page1Ctrl
and page1.html
. Are you sure you cannot access $scope.email
and $scope.password
in Page1Ctrl
.
It should be available unless you try to create a model object like this:
$scope.LoginProfile = {
email: '',
password: ''
}
and use this LoginProfile
object page1.html
as the LoginProfile.email
and LoginProfile.password
.
PS: Try to interpolate over html so you can view the values ββ(for example LoginProfile: {{LoginProfile}}
)
source to share