Angular js控制器不能正常工作

时间:2017-06-10 11:41:01

标签: angularjs angularjs-directive



Inside b dosomething1
Inside a  dosomething2

function studentController($scope) {
		$scope.student = {
			firstName : "Mahesh",
			lastName : "Parashar",
			fullName : function() {
				var studentObject;
				studentObject = $scope.student;
				return studentObject.firstName + " " + studentObject.lastName;
			}
		};
	}




我是Angular js的新手并且开始制作程序但是ng-controller不工作,当我添加了ng-controller =" studentController"正常角度ng-bind ="数据"程序也没有工作所以请任何人帮助我解决它的错误。

谢谢

Priyanka Sankhala

3 个答案:

答案 0 :(得分:2)

您的代码存在许多问题!

(i)您的角度版本以及您定义控制器的方式。您应该具有声明如下的模块名称,

ngular.module('myApp',[])

(ii)将表达式 {} 与HTML中的模型名称一起使用,例如,

{{student.firstName}}

(iii)fullName是一个函数,因此您需要调用,如

<td>{{student.fullName()}}</td>

<强>样本

angular.module('myApp',[]).controller('studentController', function($scope){
  $scope.student = {
			firstName : "Mahesh",
			lastName : "Parashar",
			fullName : function() {
				var studentObject;
				studentObject = $scope.student;
				return studentObject.firstName + " " + studentObject.lastName;
			}
		};
          
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<title>AngularJS First Application</title>
<body>
    <h1>Sample Application</h1>
    <div ng-app="myApp" ng-controller="studentController">
        <p>
            Enter your Name:<input type="text" ng-model="data">
        </p>
        <p>
            Hello <span ng-bind="data"></span>!
        </p>
        <br />
        <table border="1px">
            <tr>
                <th>Fisrt Name</th>
                <th>Last Name</th>
                <th>Full Name</th>
            </tr>
            <tr>
                <td>{{student.firstName}}</td>
                <td>{{student.lastName}}</td>
                <td>{{student.fullName()}}</td>
            </tr>
        </table>
    </div>
</body>

答案 1 :(得分:0)

您应该为角度定义应用并在其下添加控制器

    angular.module('app', []).controller('studentController', function($scope) {
  $scope.student = {
        firstName : "Mahesh",
        lastName : "Parashar",
        fullName : function() {
            var studentObject;
            studentObject = $scope.student;
            return studentObject.firstName + " " + studentObject.lastName;
        }
    };
});

然后在ng-app

中添加应用名称

答案 2 :(得分:0)

我尝试稍微修改这个程序以使其正常工作,代码如下所示。

JavaScript.js

case Constants.MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;
    // construct a string from the valid bytes in the buffer
    String readMessage = new String(readBuf, 0, msg.arg1);
    if(readMessage.equals("message required")){
        mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + readMessage);
    }
    break;

HtmlPage

    var app = angular.module("app", [])
    .controller("studentController", studentController);

    function studentController($scope) {
    $scope.student = {
        firstName: "Mahesh",
        lastName: "Parashar",
    };
    $scope.getFullName = function () {
        var studentObject;
        studentObject = $scope.student;
        //return studentObject.firstName + " " + studentObject.lastName;
        $scope.student.fullName = studentObject.firstName + " " + studentObject.lastName;
    }
}

如果您需要,请告诉我是否适合您。