R
R
relows2019-05-22 14:36:25
Angular
relows, 2019-05-22 14:36:25

How does a factory work?

https://www.youtube.com/watch?v=2tx1B8zXNqo&list=P...
This tutorial says that

<!DOCTYPE html>
<html ng-app='app' lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.min.js"></script>
    <script src="script.js"></script>

    <div ng-controller='myCtrl'>
            items: <input ng-model='i' min="0" step="1" type="number"><br>
            count: <input ng-model='c' min="0" step="1" type="number"><br>
            price: {{i * c}} {{hello}}
    </div>
</body>
</html>

var ang = angular.module('app', []);

ang.controller('myCtrl', function($scope, myFactory) {
    $scope.myFactory = myFactory;
    $scope.i = 1;
    $scope.c = 20 ;
});

ang.factory('myFactory', function() {
    return {
        hello: '$'
    };
});

with this code in the myCtrl controller, the data from myFactory should be displayed.
Nothing happens to me.
In general, I would like to know the solution to the problem, but it is much better to understand how factories work in AngularJs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Luzanov, 2019-05-22
@relows

You refer to the {{hello}} variable in the template, but your factory is stored in myFactory. Should be {{myFactory.hello}}.
How factories work - a lot of material on the net. If you need to learn, better switch to the new version of Angular.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question