Reputation: 66560
I'm learning Angular.js and I set <title>{{title}}</title>
and I try to change that using select element
<select ng-model="user.title">
<option value="Lorem">Lorem</option>
<option value="Ipsum">Ipsum</option>
<option value="Dolor">Dolor</option>
</select>
I try ng-change
and ng-select
with set()
function ctrl($scope) {
$scope.title = 'hello'; // this set the title
$scope.set = function() {
$scope.title = $scope.user.title; // this not
};
}
THe function don't work, but it work when I just set it without a function.
I also try to create change directive:
app.directive('change', function() {
console.log('change');
return {
link: function(scope, element, attrs) {
console.log(arguments);
element[0].onChange = function() {
scope[attrs[0]]();
};
}
};
});
but this too don't work. Console.log don't execute at all.
Upvotes: 10
Views: 18731
Reputation: 629
When dealing with the title tag you should use ng-bind-template so you don't see the expression in its raw state {{someVar}}
before Angular has a chance to kick in. You can add the initial text of the title within the title tag and it will be overwritten by your template.
<html data-ng-app="app">
<head>
<title ng-bind-template="My App - {{title}}">My App - Default Title</title>
</head>
<body>
{{title}}
<select ng-model="title">
<option value="Lorem">Lorem</option>
<option value="Ipsum">Ipsum</option>
<option value="Dolor">Dolor</option>
</select>
</body>
</html>
Upvotes: 19
Reputation: 4317
I figured out one solution to set title in <title>
tag.
main.js :
app = angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', '$locationProvider', function($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.
when("/faq", {
controller: "MYCtrl",
templateUrl: "../assets/templates/faq.html",
title: "FAQ"
});
}]);
app.run(['$location', '$rootScope', function( $location, $rootScope ){
$rootScope.$on("$routeChangeSuccess", function(event, currentRoute, previousRoute) {
$rootScope.title = currentRoute.title;
});
}]);
application.html.erb:
<title ng-bind="'MYAPP - ' + $root.title"></title>
Upvotes: 3
Reputation: 128
Use $rootScope
.when('/', {
templateUrl: '/templates/page/home.html',
controller: ['$scope','$rootScope', function ($scope,$rootScope) {
$rootScope.title = 'Учитель24.рф';
}]});
Upvotes: 4
Reputation: 54524
Since @madhead gave you an awesome answer, I just want to shed some light on your question about why your code doesn't work.
You can definitely use ng-change
, please take a look at this code. Your approach is very close, and I guess maybe you missed something?
<ul ng-app="myapp" ng-controller="MainCtrl">
<div>{{title}}</div>
<select ng-model="user.title" ng-change="set()">
<option value="Lorem">Lorem</option>
<option value="Ipsum">Ipsum</option>
<option value="Dolor">Dolor</option>
</select>
</ul>
function MainCtrl($scope) {
$scope.set = function () {
$scope.title = $scope.user.title;
};
}
I want to point out one thing. If you test in jsfiddle, don't use <title>{{title}}</title>
, use <div>{{title}}</div>
instead. Somehow title tag will not show correctly in the demo window.
Upvotes: 3
Reputation: 33422
Everything should work fine without extra code:
<html data-ng-app="app">
<head>
<title>{{title}}</title>
</head>
<body>
{{title}}
<select ng-model="title">
<option value="Lorem">Lorem</option>
<option value="Ipsum">Ipsum</option>
<option value="Dolor">Dolor</option>
</select>
</body>
</html>
And that's all. Here is a pen.
Upvotes: 2