0

为什么下面的输入按钮不调用控制器login()内的功能InitCtrl

<html lang="en" ng-app="mvc-module" class="ng-scope"><head>
    <meta charset="utf-8">
    <title>Sign in</title>
    <script src="/SpringMVC/static/todo.js"></script>
</head>
<body>
<div ng-controller="InitCtrl" ng-bind-html-unsafe="mainPage" class="ng-scope ng-binding">
    <!------ this part of code is injected by binding model of Angularjs --> 
<div class="container">
    <input type="button" ng-click="login()" value="Sign in" class="btn btn-large btn-primary">
</div>
    <!--- end of injection --->
</div>
</body></html>

这是todo.js

function InitCtrl($scope, $http) {

    $scope.login = function () {
    console.log("In login!");
    $http.post("login", {password: $scope.password, username: $scope.username}).
        success(function (data, status, headers, config) {
            $scope.mainPage = data;
            console.log("successfully logged to login");
        }).error(function (data, status, headers, config) {
            console.log("error in post");
        });

    };

    $http.get("login").success(function (data, status, headers, config) {
    $scope.mainPage = data;
    });
}

这不是问题的提琴手版本http://jsfiddle.net/pooyaho/M8krc/4/

4

2 回答 2

3

我们要插入 HTML,ng-bind-html-unsafe因此ng-click不起作用。为了使它工作,我们需要使用service编译这个源代码。$compile

所以让我们创建“编译器”:

.directive( 'compileData', function ( $compile ) {
  return {
    scope: true,
    link: function ( scope, element, attrs ) {

      var elmnt;

      attrs.$observe( 'template', function ( myTemplate ) {
        if ( angular.isDefined( myTemplate ) ) {
          // compile the provided template against the current scope
          elmnt = $compile( myTemplate )( scope );

            element.html(""); // dummy "clear"

          element.append( elmnt );
        }
      });
    }
  };
});

之后,让我们创建factory一个模拟服务器响应的虚拟对象:

.factory( 'tempService', function () {
  return function () {

    // $http.post("login", {password: $scope.password, username: $scope.username}).
     //   success(function (data, status, headers, config) {
    //       
     //       console.log("successfully logged to login");
     //       return data;
     //   }).error(function (data, status, headers, config) {
      //      console.log("error in post");
      //      return "error";   
      //  });


    // obviously would be $http      
    return '<form class= "form-signin" >' +
        '<h2 class="form-signin-heading">Please sign in</h2>' +
        '<input type = "text" class= "input-block-level" placeholder = "User name" ng-model = "username" >' +
        '<input type="password" class="input-block-level" placeholder="Password" ng-model="password">' +
        '<label class="checkbox">' +
        '<input type="checkbox" value="remember-me" ng-model="remember"> Remember me' +
        '</label>' +
        '<input type="button" class="btn btn-large btn-primary" ng-click="login()" value="Sign in">' +
        '</form>';
  };
});

最后让我们将指令添加到 HTML 中:

 <div compile-data template="{{mainPage}}"></div>

当然,我们需要在控制器中注册我们的factoryand directive

$scope.mainPage= tempService();

您可以在此处找到工作示例:FIDDLE

于 2013-09-30T14:15:22.497 回答
1

ng-bind-html-unsafe在较新版本的 angular-js 中被删除,如果你使用它会抛出错误。

于 2013-09-30T14:43:04.833 回答