Angularjs Angularj同样的例子适用于在线演示,但在fiddle中失败。有什么好处?

Angularjs Angularj同样的例子适用于在线演示,但在fiddle中失败。有什么好处?,angularjs,angularjs-scope,Angularjs,Angularjs Scope,此代码示例在此小提琴中不起作用,但在同一作者的演示中起作用:。 因为我的小提琴也不成功,所以我很感激任何关于发生了什么的见解 <!DOCTYPE html> <html ng-app> <head> <title>Hello World, AngularJS - ViralPatel.net</title> <script type="text/javascript" src="http://ajax.google

此代码示例在此小提琴中不起作用,但在同一作者的演示中起作用:。 因为我的小提琴也不成功,所以我很感激任何关于发生了什么的见解

    <!DOCTYPE html>
<html ng-app>
<head>
<title>Hello World, AngularJS - ViralPatel.net</title>
<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div ng-controller="ContactController">
     Email:<input type="text" ng-model="newcontact"/>
    <button ng-click="add()">Add</button>
    <h2>Contacts</h2>

    <ul>
        <li ng-repeat="contact in contacts"> {{ contact }} </li>
    </ul>

</div>
<script type="text/javascript">
    function ContactController($scope) {
        $scope.contacts = ["hi@email.com", "hello@email.com"];

        $scope.add = function() {
        $scope.contacts.push($scope.newcontact);
        $scope.newcontact = "";
        }
    }
</script>
</body>
</html>

世界你好,AngularJS-ViralPatel.net
电邮:
添加
联络
  • {{contact}
功能控制器($scope){ $scope.contacts=[”hi@email.com", "hello@email.com"]; $scope.add=函数(){ $scope.contacts.push($scope.newcontact); $scope.newcontact=“”; } }
您的
输入的
ng型号
联系人
,在它要查找的函数中
$scope.newcontact
。改变这一点,它可以工作:

功能联系人控制器($scope){
$scope.contacts=[“viralpatel。net@gmail.com", "hello@email.com"];
$scope.add=函数(){
$scope.contacts.push($scope.contact);//在此处更改
$scope.contact=”“;//此处已更改
}
}

您的小提琴有一个绑定到作用域的不同变量
contact
。您要输入联系人数组的变量是
newcontact
,该变量未在范围中定义。为了清晰起见,我更改了对该变量的所有引用,并将其命名为
newContact

这把小提琴会起作用:

function ContactController($scope) {
    $scope.contacts = ["viralpatel.net@gmail.com", "hello@email.com"];

    $scope.add = function() {
        $scope.contacts.push($scope.contact); //Changed here
        $scope.contact = ""; //Changed here
    }
}

<input type="text" ng-model="contact" />
<div ng-app="" ng-controller="ContactController">

        Email:<input type="text" ng-model="newContact" />

        <button ng-click="add()">Add</button>

        <h2>Contacts</h2>
        <ul>
            <li ng-repeat="contact in contacts"> {{ contact }} </li>
        </ul>

    </div>
function ContactController($scope) {
    $scope.contacts = ["viralpatel.net@gmail.com", "hello@email.com"];

    $scope.add = function() {
        $scope.contacts.push($scope.newContact);
        $scope.newContact = "";
    }
}