gpt4 book ai didi

javascript - 函数在angularJs中调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:03:07 26 4
gpt4 key购买 nike

我在 plunkr 上有一个演示,当 ng-click 事件函数上的点击函数通过检查控制台调用两次时,我遇到了问题:-

<a class="btn btn-default finish" style="display:none" ng-click="result()">Finish</a>

这是我的 Controller 代码:-

var app = angular.module('app', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: 'MainView.html',
controller: 'MainCtrl'
}).when('/view', {
templateUrl: 'view2.html',
controller: 'MainCtrl'
});
});
app.controller('MainCtrl', function($scope, $location) {
$scope.name = 'World';
$scope.correctAnswer = [];

$scope.result = function() {
$scope.correctAnswer.push({
"label1": "value1"
}, {
"label2": "value2"
}, {
"label3": "value3"
});
console.log($scope.correctAnswer);
$location.path("/view");
}
});

Plunker

最佳答案

哦,很简单,您正在调用 $location.path("/view"); 并且在 view2.html 中有一个 ng -init 执行相同的 result() 功能:

<div class="tab-pane fade" id="profile" ng-init="result()">

这是 updated Plunker使用一种可能的(快速)解决方案。

关于javascript - 函数在angularJs中调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31447696/

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