gpt4 book ai didi

javascript - 当使用 ng-bind-html-unsafe 绑定(bind)模板时,ng-click 不起作用

转载 作者:行者123 更新时间:2023-11-28 15:55:25 26 4
gpt4 key购买 nike

为什么下面的输入按钮不调用InitCtrl Controller 内的login()函数?

<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;
});
}

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

最佳答案

我们想要使用 ng-bind-html-unsafe 插入 HTML,因此 ng-click 不起作用。为了使其工作,我们需要使用 $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( '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>

当然,我们需要在 Controller 中注册我们的工厂指令:

$scope.mainPage= tempService();

您可以在这里找到工作示例: FIDDLE

关于javascript - 当使用 ng-bind-html-unsafe 绑定(bind)模板时,ng-click 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19096239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com