AngularJS函数调用导致应用程序停止

时间:2016-07-26 10:58:26

标签: javascript angularjs

看看这段代码:

// Start AngularJS Application
var application = angular.module('engineers', []);
// Set controller
application.controller('engineers_controller', function($scope) {
    // Seting page meta information
    $scope.description = 'Some Description', $scope.keywords = 'key1,key2,key3', $scope.application_name = 'Sarabon', $scope.googleplus_url = '#', $scope.page_title = 'مهندسین مشاور سرابن',
    $scope.txt1="Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows. Some pilots get picked and become television programs. Some don't, become nothing. She starred in one of the ones that became nothing.",
    $scope.txt2="You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man.",
    $scope.switch1=function(){
        $scope.activetext=$scope.txt1,
        $scope.firstswitcher='fa fa-square';
        $scope.secondswitcher='fa fa-square-o';
    },
    $scope.switch2=function(){
        $scope.activetext=$scope.txt2,
        $scope.secondswitcher='fa fa-square';
        $scope.firstswitcher='fa fa-square-o';
    };
    switch1();
});

我尝试拨打switch1()函数onpageloads。但是当我运行我的页面时,几乎所有页面都被破坏而且无法正常工作。当我从代码中删除switch1();时,它再次正常工作。代码有什么问题?我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

函数$('...').flipster()不存在。您可能打算改为编写switch1

答案 1 :(得分:0)

当您在控制器上调用switch1()时,实际执行的内容是$scope.switch1(),这是正确的方法,这就是您应该调用的内容。正如你所说,删除switch1()会让你的代码再次运行,所以你已经为自己解答了这个问题。