简体   繁体   English

AngularJS控制器功能不起作用

[英]Angularjs controller function not working

I am new to Angularjs. 我是Angularjs的新手。 I created a small program to take input on form-submit and add it to json data using a controller function. 我创建了一个小程序来接受表单提交的输入,并使用控制器功能将其添加到json数据中。

It seems like a small error in program but I am unable to locate it. 似乎是程序中的一个小错误,但我找不到它。

Please help: below are my angjs.html ( which has the form defined in it ), and app.js ( for the controllers and json data ): 请帮忙:以下是我的angjs.html (其中定义了表单)和app.js (用于控制器和json数据):

angjs.html angjs.html

<body>
    <div ng-controller="loginController as login">
        <div ng-repeat="user in login.users">
            <h2>{{user.username}}</h2>
            <h3>{{user.password}}</h3>
        </div>
        <form name="loginUser" ng-controller="FeedDataController as feedCtrl" ng-submit="feedCtrl.addUser(user)">
            <!-- Live preview -->
            <div>
                <h2>{{feedCtrl.user.username}}</h2>
                <h3>{{feedCtrl.user.password}}</h3>
            </div>
            <!-- form submit -->
            <input type="text" ng-model="feedCtrl.user.username" name="username" /><br />
            <input type="text" ng-model="feedCtrl.user.password" name="password" /><br />
            <input type="submit" value="GO" />
        </form>
    </div>
</body>

app.js app.js

(function(){
var app = angular.module('login',[]);

app.controller('loginController',function(){
    this.users = user;
});

app.controller('FeedDataController',function(){
    this.user={};
    this.addUser=function(user){
        user.push(this.user);
        this.user={};
    };
});

var user = [
{username: 'Azurite',    password: 'hello',    canPurchase: false,    soldOut: true  },
{username: 'Blinge',    password: 'right',    canPurchase: false,    soldOut: true  }
];

you have a typo between user and users 您在用户之间错别字

plunker : http://plnkr.co/edit/tn5pwxidy0uf3rnlGUHT?p=preview plunker: http ://plnkr.co/edit/tn5pwxidy0uf3rnlGUHT?p = preview

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

app.controller('loginController',function(){
    this.users = users;
});

app.controller('FeedDataController',function(){
    this.user={};
    this.addUser=function(){
        users.push(this.user);
        this.user={};
    };
});

var users = [
  {username: 'Azurite',    password: 'hello',    canPurchase: false,    soldOut: true  },
  {username: 'Blinge',    password: 'right',    canPurchase: false,    soldOut: true  }
];
}); 

In ng-submit="feedCtrl.addUser(user)" you use user , which is undefined. ng-submit="feedCtrl.addUser(user)" ,使用user ,该user未定义。 Change to ng-submit="feedCtrl.addUser(feedCtrl.user)" 更改为ng-submit="feedCtrl.addUser(feedCtrl.user)"

You haven't properly closed you IIFE function in app.js: 您尚未在app.js中正确关闭IIFE函数:

(function(){
var app = angular.module('login', []);

app.controller('loginController',function(){
    //var vm = this;
    this.users = user;
});

app.controller('FeedDataController',function(){
    //var vm = this;    
    this.user={};
    this.addUser=function(user){
        user.push(this.user);
        this.user={};
    };
});

var user = [
{username: 'Azurite',    password: 'hello',    canPurchase: false,    soldOut: true  },
{username: 'Blinge',    password: 'right',    canPurchase: false,    soldOut: true  }
];

})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM