Angular controller not working

I am new to angular My angular controller:

var chatApp = angular.module('chatApp.controllers', []);

chatApp.controller('loginController', ['$scope', function($scope) {
$scope.user_name="name";
$scope.user_password="name";
}]);

app.js:

'use strict';
angular.module('chatApp', [
'ui.router',
'chatApp.filters',
'chatApp.services',
'chatApp.directives',
'chatApp.controllers'
]).config(function($stateProvider, $urlRouterProvider) {
$stateProvider
    .state('login', {
        url: "/login",
        templateUrl: "partials/login.html",
        controller : "loginController"
    })
});

login.html:

<div>
<div id="wrapper">
    <form name="login-form" class="login-form" action="" method="post">
        <div class="header">
            <h1>Login Form</h1>
        </div>
        <div class="content">
            <input name="username" type="text" class="input username" placeholder="{{user_name}}" />
            <div class="user-icon"></div>
            <input name="password" type="password" class="input password" placeholder="{{user_password}}" />
            <div class="pass-icon"></div>
        </div>
    </form>
</div>

I used stateprovider for routing. But it shows nothing in the placeholder. What is the reason?

+4
source share
5 answers

This works for me if I say ng-controller = "loginController" in html -

<div ng-controller = 'loginController'>
<div id="wrapper">
    <form name="login-form" class="login-form" action="" method="post">
        <div class="header">
            <h1>Login Form</h1>
        </div>
        <div class="content">
            <input name="username" type="text" class="input username" placeholder="{{user_name}}" />
            <div class="user-icon"></div>
            <input name="password" type="password" class="input password" placeholder="{{user_password}}" />
            <div class="pass-icon"></div>
        </div>
</div>

JSBin works here: http://jsbin.com/aDuJIku/265/edit?html,js,output

This solution does not include ui-router, but it should work with it.

0
source

Try the following:

 var chatApp = angular.module('chatApp.controllers', []);

    chatApp.controller('loginController', ['$scope','$timeout', function($scope,$timeout) {

        $timeout(function(){
            $scope.user_name="name";
            $scope.user_password="name";
        },100);

    }]);
0
source

ng- {{}}.

<div class="content">
            <input name="username" type="text" class="input username" placeholder="username" ng-model="user_name" />
            <div class="user-icon"></div>
            <input name="password" type="password" class="input password" placeholder="password" ng-model="user_password"/>
            <div class="pass-icon"></div>
        </div>
0

RouteProvider. :

angular.module('chatApp', [ 'ngRoute' 'ui.router','chatApp.filters','chatApp.services', 'chatApp.directives', 'chatApp.controllers'])
.config(function($routeProvider) {
    $routeProvider.when('/login', { templateUrl: "partials/login.html", controller: "loginController" })
});

, angular -route js.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-route.js"></script>

, !;)

this

-1

.

Note. If someone tries to run this code in a preview, it will not work. JS and HTML code must be copied to a separate file and use the specified names.

var myContrlApp = angular.module('ContrlApp', []);
myContrlApp.controller('MainController', function ($scope) {
    var person = {
        firstName: "Keith",
        lastName: "Jackson",
    };
    $scope.message = "Welcome, Angular !";
    $scope.person = person;
});
<html ng-app="ContrlApp">
<head>
    <script type="text/javascript" src="Scripts/angular.js"></script>
    <script type="text/javascript" src="Scripts/angular-route.js"></script>
    <script type="text/javascript" src="Scripts/script.js"></script>
    <!--<link rel="stylesheet" href="style.css" />-->
    <title>Angular JS Tryout</title>
</head>
<body ng-controller="MainController">
    <div>
        <h1>{{message}}</h1>
        <div>
            <div>First name: {{person.firstName}}</div>
            <div>Last name: {{person.lastName}}</div>

        </div>
    </div>
</body>
</html>
Run codeHide result
-1
source

Source: https://habr.com/ru/post/1534383/


All Articles